Java Code Examples for javax.jms.TextMessage#setObjectProperty()

The following examples show how to use javax.jms.TextMessage#setObjectProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JmsSourceTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithString() throws JMSException {
    WeldContainer container = prepare();

    RawMessageConsumerBean bean = container.select(RawMessageConsumerBean.class).get();
    assertThat(bean.messages()).isEmpty();

    Queue q = jms.createQueue("queue-one");
    JMSProducer producer = jms.createProducer();
    TextMessage message = jms.createTextMessage("hello");
    message.setStringProperty("string", "value");
    message.setBooleanProperty("bool", true);
    message.setLongProperty("long", 100L);
    message.setByteProperty("byte", (byte) 5);
    message.setFloatProperty("float", 5.5f);
    message.setDoubleProperty("double", 10.3);
    message.setIntProperty("int", 23);
    message.setObjectProperty("object", "yop");
    message.setShortProperty("short", (short) 3);
    producer.send(q, message);

    await().until(() -> bean.messages().size() == 1);
    IncomingJmsMessage<?> incomingJmsMessage = bean.messages().get(0);
    IncomingJmsMessageMetadata metadata = incomingJmsMessage.getMetadata(IncomingJmsMessageMetadata.class)
            .orElseThrow(() -> new AssertionError("Metadata expected"));
    assertThat(incomingJmsMessage.getPayload()).isEqualTo("hello");
    assertThat(metadata.getBody(String.class)).isEqualTo("hello");
    assertThat(metadata.propertyExists("string")).isTrue();
    assertThat(metadata.propertyExists("missing")).isFalse();
    assertThat(metadata.getStringProperty("string")).isEqualTo("value");
    assertThat(metadata.getBooleanProperty("bool")).isTrue();
    assertThat(metadata.getLongProperty("long")).isEqualTo(100L);
    assertThat(metadata.getByteProperty("byte")).isEqualTo((byte) 5);
    assertThat(metadata.getFloatProperty("float")).isEqualTo(5.5f);
    assertThat(metadata.getDoubleProperty("double")).isEqualTo(10.3);
    assertThat(metadata.getIntProperty("int")).isEqualTo(23);
    assertThat(metadata.getObjectProperty("object")).isInstanceOf(String.class);
    assertThat(((String) message.getObjectProperty("object"))).isEqualTo("yop");
    assertThat(message.getShortProperty("short")).isEqualTo((short) 3);
}
 
Example 2
Source File: JmsMultipleBrokersTestSupport.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void sendMessages(String brokerName,
                            Destination destination,
                            int count,
                            HashMap<String, Object> properties) throws Exception {
   BrokerItem brokerItem = brokers.get(brokerName);

   Connection conn = brokerItem.createConnection();
   conn.start();
   Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   MessageProducer producer = brokerItem.createProducer(destination, sess);
   producer.setDeliveryMode(persistentDelivery ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);

   for (int i = 0; i < count; i++) {
      TextMessage msg = createTextMessage(sess, conn.getClientID() + ": Message-" + i);
      if (properties != null) {
         for (String propertyName : properties.keySet()) {
            msg.setObjectProperty(propertyName, properties.get(propertyName));
         }
      }
      producer.send(msg);
      onSend(i, msg);
   }

   producer.close();
   sess.close();
   conn.close();
   brokerItem.connections.remove(conn);
}
 
Example 3
Source File: PropertyFilterTest.java    From brave with Apache License 2.0 5 votes vote down vote up
static void setAllPropertyTypes(TextMessage message) throws JMSException {
  message.setBooleanProperty("boolean", true);
  message.setByteProperty("byte", Byte.MAX_VALUE);
  message.setDoubleProperty("double", Double.MIN_VALUE);
  message.setFloatProperty("float", Float.MIN_VALUE);
  message.setIntProperty("int", Integer.MIN_VALUE);
  message.setLongProperty("long", Long.MIN_VALUE);
  message.setObjectProperty("object", Collections.emptyMap());
  message.setShortProperty("short", Short.MIN_VALUE);
  message.setStringProperty("string", "string");
}
 
Example 4
Source File: JmsPropertyTypeTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testObjectProperty(String port,
                               String adminUsername,
                               String adminPassword,
                               String brokerHostname) throws NamingException, JMSException {
    String queueName = "testObjectProperty";

    InitialContext initialContextForQueue = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withQueue(queueName)
            .build();

    ConnectionFactory connectionFactory
            = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // send messages
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = producerSession.createQueue(queueName);
    MessageProducer producer = producerSession.createProducer(queue);
    String objectPropertyName = "ObjectProperty";
    String objectProperty = "123456qwertyQWERTY!@#$%^";
    TextMessage textMessage = producerSession.createTextMessage("Test message");
    textMessage.setObjectProperty(objectPropertyName, objectProperty);
    producer.send(textMessage);
    producerSession.close();

    // receive messages
    Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
    Session subscriberSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message, "Message was not received");
    String receivedObjectProperty = (String) message.getObjectProperty(objectPropertyName);
    Assert.assertEquals(objectProperty, receivedObjectProperty, "Object property not matched.");

    subscriberSession.close();
    connection.close();
}