Java Code Examples for javax.jms.JMSProducer#send()

The following examples show how to use javax.jms.JMSProducer#send() . 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: JmsProducerTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
private void doTestSendAppliesDisableMessageIDWithMessageBody(Class<?> bodyType) throws JMSException {
    JMSProducer producer = context.createProducer();

    producer.setDisableMessageID(true);
    producer.send(JMS_DESTINATION, "text");
    JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    JmsMessage message = envelope.getMessage();
    assertNull(message.getJMSMessageID());

    producer.setDisableMessageID(false);
    producer.send(JMS_DESTINATION, "text");
    envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    message = envelope.getMessage();
    assertNotNull(message.getJMSMessageID());
}
 
Example 2
Source File: JmsPoolJMSProducerTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuntimeExceptionFromSendSerializableBody() throws JMSException {
    JMSProducer producer = context.createProducer();

    MockJMSConnection connection = (MockJMSConnection) context.getConnection();
    connection.addConnectionListener(new MockJMSDefaultConnectionListener() {

        @Override
        public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
            throw new IllegalStateException("Send Failed");
        }
    });

    try {
        producer.send(context.createTemporaryQueue(), UUID.randomUUID());
        fail("Should have thrown an exception");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 3
Source File: JmsProducerTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testBytesBodyIsApplied() throws JMSException {
    JMSProducer producer = context.createProducer();

    final byte[] bodyValue = new byte[] { 0, 1, 2, 3, 4 };

    producer.send(JMS_DESTINATION, bodyValue);
    JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    JmsMessage message = envelope.getMessage();

    byte[] payload = message.getBody(byte[].class);
    assertNotNull(payload);
    assertEquals(bodyValue.length, payload.length);

    for (int i = 0; i < payload.length; ++i) {
        assertEquals(bodyValue[i], payload[i]);
    }
}
 
Example 4
Source File: JmsPoolJMSProducerTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuntimeExceptionFromSendByteBody() throws JMSException {
    JMSProducer producer = context.createProducer();

    MockJMSConnection connection = (MockJMSConnection) context.getConnection();
    connection.addConnectionListener(new MockJMSDefaultConnectionListener() {

        @Override
        public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
            throw new IllegalStateException("Send Failed");
        }
    });

    try {
        producer.send(context.createTemporaryQueue(), new byte[0]);
        fail("Should have thrown an exception");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 5
Source File: JmsProducerTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
private void doTestSendAppliesDisableTimestampWithMessageBody(Class<?> bodyType) throws JMSException {
    JMSProducer producer = context.createProducer();

    producer.setDisableMessageTimestamp(true);
    producer.send(JMS_DESTINATION, "text");
    JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    JmsMessage message = envelope.getMessage();
    assertTrue(message.getJMSTimestamp() == 0);

    producer.setDisableMessageTimestamp(false);
    producer.send(JMS_DESTINATION, "text");
    envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    message = envelope.getMessage();
    assertFalse(message.getJMSTimestamp() == 0);
}
 
Example 6
Source File: JmsPoolJMSProducerTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializableBodyIsApplied() throws JMSException {
    JMSProducer producer = context.createProducer();

    final UUID bodyValue = UUID.randomUUID();
    final AtomicBoolean bodyValidated = new AtomicBoolean();

    MockJMSConnection connection = (MockJMSConnection) context.getConnection();
    connection.addConnectionListener(new MockJMSDefaultConnectionListener() {

        @Override
        public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
            assertEquals(bodyValue, message.getBody(UUID.class));
            bodyValidated.set(true);
        }
    });

    producer.send(JMS_DESTINATION, bodyValue);
    assertTrue(bodyValidated.get());
}
 
Example 7
Source File: JmsProducerTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
private void doTestSendAppliesTimeToLiveWithMessageBody(Class<?> bodyType) throws JMSException {
    JMSProducer producer = context.createProducer();

    producer.setTimeToLive(2000);
    producer.send(JMS_DESTINATION, "text");
    JmsOutboundMessageDispatch envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    JmsMessage message = envelope.getMessage();
    assertTrue(message.getJMSExpiration() > 0);

    producer.setTimeToLive(Message.DEFAULT_TIME_TO_LIVE);
    producer.send(JMS_DESTINATION, "text");
    envelope = remotePeer.getLastReceivedMessage();
    assertNotNull(envelope);
    message = envelope.getMessage();
    assertTrue(message.getJMSExpiration() == 0);
}
 
Example 8
Source File: JmsPoolJMSProducerTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
private void doTestSendAppliesTimeToLiveWithMessageBody(Class<?> bodyType) throws JMSException {
    JMSProducer producer = context.createProducer();

    final AtomicBoolean nonDefaultTTL = new AtomicBoolean();
    final AtomicBoolean defaultTTL = new AtomicBoolean();

    MockJMSConnection connection = (MockJMSConnection) context.getConnection();
    connection.addConnectionListener(new MockJMSDefaultConnectionListener() {

        @Override
        public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
            if (!nonDefaultTTL.get()) {
                assertTrue(message.getJMSExpiration() > 0);
                nonDefaultTTL.set(true);
            } else {
                assertTrue(message.getJMSExpiration() == 0);
                defaultTTL.set(true);
            }
        }
    });

    producer.setTimeToLive(2000);
    producer.send(JMS_DESTINATION, "text");

    producer.setTimeToLive(Message.DEFAULT_TIME_TO_LIVE);
    producer.send(JMS_DESTINATION, "text");

    assertTrue(nonDefaultTTL.get());
    assertTrue(defaultTTL.get());
}
 
Example 9
Source File: JmsProducerTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void sendWithBodyOfType(JMSProducer producer, Class<?> asType) {
    if (asType.equals(String.class)) {
        producer.send(JMS_DESTINATION, "String-body-type");
    } else if (asType.equals(Map.class)) {
        producer.send(JMS_DESTINATION, Collections.<String, Object>emptyMap());
    } else if (asType.equals(Message.class)) {
        producer.send(JMS_DESTINATION, context.createMessage());
    } else if (asType.equals(byte[].class)) {
        producer.send(JMS_DESTINATION, new byte[10]);
    } else if (asType.equals(UUID.class)) {
        producer.send(JMS_DESTINATION, UUID.randomUUID());
    }
}
 
Example 10
Source File: JmsContextTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidDestination() {
   JMSProducer producer = context.createProducer();
   Message msg = context.createMessage();
   try {
      producer.send((Destination) null, msg);
      Assert.fail("null Destination");
   } catch (InvalidDestinationRuntimeException expected) {
      // no-op
   }
}
 
Example 11
Source File: JmsPoolJMSProducerTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testBytesBodyIsApplied() throws JMSException {
    JMSProducer producer = context.createProducer();

    final byte[] bodyValue = new byte[] { 0, 1, 2, 3, 4 };
    final AtomicBoolean bodyValidated = new AtomicBoolean();

    MockJMSConnection connection = (MockJMSConnection) context.getConnection();
    connection.addConnectionListener(new MockJMSDefaultConnectionListener() {

        @Override
        public void onMessageSend(MockJMSSession session, Message message) throws JMSException {
            byte[] payload = message.getBody(byte[].class);
            assertNotNull(payload);
            assertEquals(bodyValue.length, payload.length);

            for (int i = 0; i < payload.length; ++i) {
                assertEquals(bodyValue[i], payload[i]);
            }

            bodyValidated.set(true);
        }
    });

    producer.send(JMS_DESTINATION, bodyValue);
    assertTrue(bodyValidated.get());
}
 
Example 12
Source File: GroupingTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void send(JMSContext ctx, Queue testQueue, String groupID1, JMSProducer producer1, int j) throws JMSException {
   TextMessage message = ctx.createTextMessage("Message" + j);
   producer1.send(testQueue, message);
   String prop = message.getStringProperty("JMSXGroupID");
   assertNotNull(prop);
   assertEquals(groupID1, prop);
}
 
Example 13
Source File: JmsContextTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetClientIdLate() {
   JMSProducer producer = context.createProducer();
   Message msg = context.createMessage();
   producer.send(queue1, msg);
   try {
      context.setClientID("id");
      Assert.fail("expected exception");
   } catch (IllegalStateRuntimeException e) {
      // no op
   }
}
 
Example 14
Source File: JmsContextTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseSecondContextConnectionRemainsOpen() throws JMSException {
   JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
   Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode());
   JMSProducer producer = localContext.createProducer();
   JMSConsumer consumer = localContext.createConsumer(queue1);

   final int pass = 1;
   for (int idx = 0; idx < 2; idx++) {
      Message m = localContext.createMessage();
      int intProperty = random.nextInt();
      m.setIntProperty("random", intProperty);
      Assert.assertNotNull(m);
      producer.send(queue1, m);
      m = null;
      Message msg = consumer.receive(100);
      Assert.assertNotNull("must have a msg", msg);
      Assert.assertEquals(intProperty, msg.getIntProperty("random"));
      /* In the second pass we close the connection before ack'ing */
      if (idx == pass) {
         localContext.close();
      }
      /**
       * From {@code JMSContext.close()}'s javadoc:<br/>
       * Invoking the {@code acknowledge} method of a received message from a closed connection's
       * session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection
       * must NOT throw an exception.
       */
      try {
         msg.acknowledge();
         Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx);
      } catch (javax.jms.IllegalStateException expected) {
         // HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a
         // IllegalStateRuntimeException here. But Message.ack...() says it must throws the
         // non-runtime variant.
         Assert.assertEquals("we only close the connection on pass " + pass, pass, idx);
      }
   }
}
 
Example 15
Source File: JmsContextTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidMessage() {
   JMSProducer producer = context.createProducer();
   try {
      producer.send(queue1, (Message) null);
      Assert.fail("null msg");
   } catch (MessageFormatRuntimeException expected) {
      // no-op
   }
}
 
Example 16
Source File: ArtemisJmsTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueue() throws Exception {
    try (ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); JMSContext context = factory.createContext()) {
        Queue queue = context.createQueue("queue.jms");

        JMSProducer producer = context.createProducer();
        producer.send(queue, content);

        JMSConsumer consumer = context.createConsumer(queue);
        Message message = consumer.receive(5000);

        Assert.assertEquals(queue, message.getJMSDestination());
        Assert.assertEquals(content, message.getBody(String.class));
    }
}
 
Example 17
Source File: JmsSourceTest.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithLong() {
    WeldContainer container = prepare();

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

    Queue q = jms.createQueue("queue-one");
    JMSProducer producer = jms.createProducer();
    producer.send(q, 10000L);

    await().until(() -> bean.messages().size() == 1);
    IncomingJmsMessage<?> incomingJmsMessage = bean.messages().get(0);
    assertThat(incomingJmsMessage.getPayload()).isEqualTo(10000L);
}
 
Example 18
Source File: TracingJMSProducer.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override void apply(JMSProducer producer, Destination destination, Object message) {
  producer.send(destination, (String) message);
}
 
Example 19
Source File: TracingJMSProducer.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override void apply(JMSProducer producer, Destination destination, Object message) {
  producer.send(destination, (Serializable) message);
}
 
Example 20
Source File: JMSProducerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testCreateProducerAndSend() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer, SERVER_ANONYMOUS_RELAY);
        testPeer.expectBegin();

        //Expect a link to the anonymous relay node
        TargetMatcher targetMatcher = new TargetMatcher();
        targetMatcher.withAddress(nullValue());
        targetMatcher.withDynamic(nullValue());//default = false
        targetMatcher.withDurable(nullValue());//default = none/0
        testPeer.expectSenderAttach(targetMatcher, false, false);

        JMSProducer producer = context.createProducer();
        assertNotNull(producer);

        String topicName = "testCreateProducerAndSend";
        Topic topic = context.createTopic(topicName);

        // Verify sent message contains expected destination address and dest type annotation
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        Symbol annotationKey = AmqpDestinationHelper.JMS_DEST_TYPE_MSG_ANNOTATION_SYMBOL;
        msgAnnotationsMatcher.withEntry(annotationKey, equalTo(AmqpDestinationHelper.TOPIC_TYPE));

        MessagePropertiesSectionMatcher propsMatcher = new MessagePropertiesSectionMatcher(true).withTo(equalTo(topicName));

        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(new MessageHeaderSectionMatcher(true).withDurable(equalTo(true)));
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propsMatcher);

        testPeer.expectTransfer(messageMatcher);

        Message message = context.createMessage();
        producer.send(topic, message);

        testPeer.expectEnd();
        testPeer.expectClose();
        context.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}