Java Code Examples for javax.jms.JMSContext#createTextMessage()

The following examples show how to use javax.jms.JMSContext#createTextMessage() . 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: JMSContextTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testJMSContextConsumerThrowsMessageFormatExceptionOnMalformedBody() throws Exception {
   Queue queue = createQueue(true, "ContextMalformedBodyTestQueue");

   JMSContext context = qraConnectionFactory.createContext();
   JMSProducer producer = context.createProducer();

   TextMessage message = context.createTextMessage("TestMessage");
   producer.send(queue, message);

   JMSConsumer consumer = context.createConsumer(queue);

   try {
      consumer.receiveBody(Boolean.class);
      fail("Should thrown MessageFormatException");
   } catch (MessageFormatRuntimeException mfre) {
      // Do nothing test passed
   } catch (Exception e) {
      fail("Threw wrong exception, should be MessageFormatRuntimeException, instead got: " + e.getClass().getCanonicalName());
   }
}
 
Example 2
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateTextMessage() {
    JMSContext context = cf.createContext();
    assertNotNull(context.createTextMessage());

    context.close();
    try {
        context.createTextMessage();
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 3
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateTextMessageWithPayload() {
    JMSContext context = cf.createContext();
    assertNotNull(context.createTextMessage("body"));

    context.close();
    try {
        context.createTextMessage("body");
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 4
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 5
Source File: JMSTestBase.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected final void sendMessages(JMSContext context, JMSProducer producer, Queue queue, final int total) {
   try {
      for (int j = 0; j < total; j++) {
         StringBuilder sb = new StringBuilder();
         for (int m = 0; m < 200; m++) {
            sb.append(random.nextLong());
         }
         Message msg = context.createTextMessage(sb.toString());
         msg.setIntProperty("counter", j);
         producer.send(queue, msg);
      }
   } catch (JMSException cause) {
      throw new JMSRuntimeException(cause.getMessage(), cause.getErrorCode(), cause);
   }
}
 
Example 6
Source File: ConverterMessageBuilder.java    From kafka-connect-mq-sink with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the JMS message for the Kafka Connect SinkRecord.
 * 
 * @param context            the JMS context to use for building messages
 * @param record             the Kafka Connect SinkRecord
 * 
 * @return the JMS message
 */
@Override public Message getJMSMessage(JMSContext jmsCtxt, SinkRecord record) {
    byte[] payload = converter.fromConnectData(record.topic(), record.valueSchema(), record.value());
    return jmsCtxt.createTextMessage(new String(payload, UTF_8));
}
 
Example 7
Source File: JsonMessageBuilder.java    From kafka-connect-mq-sink with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the JMS message for the Kafka Connect SinkRecord.
 * 
 * @param context            the JMS context to use for building messages
 * @param record             the Kafka Connect SinkRecord
 * 
 * @return the JMS message
 */
@Override public Message getJMSMessage(JMSContext jmsCtxt, SinkRecord record) {
    byte[] payload = converter.fromConnectData(record.topic(), record.valueSchema(), record.value());
    return jmsCtxt.createTextMessage(new String(payload, UTF_8));
}
 
Example 8
Source File: JmsContextTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testContextStopAndCloseFromMessageListeners() throws Exception {
   final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE);
   JMSConsumer consumer1 = context1.createConsumer(queue1);

   final CountDownLatch latch1 = new CountDownLatch(1);

   InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1);

   consumer1.setMessageListener(listener1);

   JMSProducer producer = context1.createProducer();
   Message msg = context1.createTextMessage("first message");
   producer.send(queue1, msg);

   latch1.await();

   Throwable error1 = listener1.getError();

   assertNotNull(error1);

   assertTrue(error1 instanceof IllegalStateRuntimeException);

   context1.close();

   final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE);
   JMSConsumer consumer2 = context2.createConsumer(queue1);

   final CountDownLatch latch2 = new CountDownLatch(1);

   InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2);

   consumer2.setMessageListener(listener2);

   JMSProducer producer2 = context2.createProducer();
   Message msg2 = context2.createTextMessage("second message");
   producer2.send(queue1, msg2);

   latch2.await();

   Throwable error2 = listener2.getError();

   assertNotNull(error2);

   assertTrue(error2 instanceof IllegalStateRuntimeException);

   context2.close();
}