Search in sources :

Example 11 with TextMessage

use of javax.jms.TextMessage in project quickstarts by jboss-switchyard.

the class ActiveMQClient method main.

/**
     * Only execution point for this application.
     * @param ignored not used.
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
    if (args.length != 0) {
        orders = args;
    }
    ActiveMQMixIn mixIn = new ActiveMQMixIn();
    try {
        Session session = mixIn.getSession();
        MessageProducer producer = session.createProducer(session.createQueue(ORDER_QUEUE));
        for (String order : orders) {
            final TextMessage message = session.createTextMessage();
            message.setText(order);
            producer.send(message);
        }
        session.close();
        session = mixIn.getSession();
        System.out.println("* * *  SHIPPING ORDERS  * * *");
        MessageConsumer consumer = session.createConsumer(session.createQueue(SHIPPING_QUEUE));
        Message msg = null;
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        System.out.println();
        System.out.println("* * *  PENDING ORDERS (FILLING STOCK)  * * *");
        consumer = session.createConsumer(session.createQueue(FILLING_STOCK_QUEUE));
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        session.close();
        Thread.sleep(2000);
    } finally {
        mixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) ActiveMQMixIn(org.switchyard.component.test.mixins.activemq.ActiveMQMixIn) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 12 with TextMessage

use of javax.jms.TextMessage in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePush.

@Test
public void testQueuePush() throws Exception {
    // Given
    final String testQueueName = "TestPushQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    final CountDownLatch consumerLatch = new CountDownLatch(1);
    final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = producerSession.createProducer(testQueue);
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create consumer
    ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    MessageConsumer consumer = consumerSession.createConsumer(testQueue);
    consumer.setMessageListener(new AssertTextMessageListener(consumerLatch, consumerThrowables, expectedTextMessage));
    // When
    producer.send(expectedTextMessage);
    consumerLatch.await(1L, TimeUnit.SECONDS);
    // Then
    assertNoConsumerError(consumerThrowables);
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(2, 5000L);
    // trace count : 1
    verifyProducerSendEvent(testQueue);
    // trace count : 1
    verifyConsumerPushEvent(testQueue);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQSession(org.apache.activemq.ActiveMQSession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) AssertTextMessageListener(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.AssertTextMessageListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 13 with TextMessage

use of javax.jms.TextMessage in project pinpoint by naver.

the class ActiveMQClientITBase method testTopicPush.

@Test
public void testTopicPush() throws Exception {
    // Given
    final String testTopicName = "TestPushTopic";
    final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
    final String testMessage = "Hello World for Topic!";
    final int numMessageConsumers = 2;
    final CountDownLatch consumerConsumeLatch = new CountDownLatch(numMessageConsumers);
    final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build();
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create 2 consumers
    ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
    ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
    // When
    producer.send(expectedTextMessage);
    consumerConsumeLatch.await(1L, TimeUnit.SECONDS);
    // Then
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(3, 1000L);
    // trace count : 1
    verifyProducerSendEvent(testTopic);
    // trace count : 1
    verifyConsumerPushEvent(testTopic);
    // trace count : 1
    verifyConsumerPushEvent(testTopic);
}
Also used : ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.MessageConsumerBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) MessageProducerBuilder(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.MessageProducerBuilder) ActiveMQSession(org.apache.activemq.ActiveMQSession) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) AssertTextMessageListener(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.AssertTextMessageListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 14 with TextMessage

use of javax.jms.TextMessage in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePull.

@Test
public void testQueuePull() throws Exception {
    // Given
    final String testQueueName = "TestPullQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = producerSession.createProducer(testQueue);
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // When
    ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    MessageConsumer consumer = consumerSession.createConsumer(testQueue);
    // Then
    producer.send(expectedTextMessage);
    Message message = consumer.receive(1000L);
    Assert.assertEquals(testMessage, ((TextMessage) message).getText());
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(5, 5000L);
    // trace count : 1
    verifyProducerSendEvent(testQueue);
    // trace count : 4
    verifyConsumerPullEvent(testQueue, consumer, expectedTextMessage);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQSession(org.apache.activemq.ActiveMQSession) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 15 with TextMessage

use of javax.jms.TextMessage in project meteo by pierre.

the class TopicListener method onMessage.

@Override
public void onMessage(Message message) {
    if (message instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) message;
        String txt = null;
        try {
            txt = txtMsg.getText();
            log.debug("Received a message, yay!\n" + txt);
            Map event = mapper.readValue(txt, Map.class);
            esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
        } catch (JMSException ex) {
            log.warn("Got an error from the message queue", ex);
        } catch (ClassCastException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonMappingException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonParseException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (IOException ex) {
            log.warn("Got an error from the message queue", ex);
        }
    } else if (message instanceof BytesMessage) {
        final BytesMessage byteMessage = (BytesMessage) message;
        long llen;
        try {
            llen = byteMessage.getBodyLength();
        } catch (JMSException e) {
            log.warn("Unable to get message length", e);
            return;
        }
        if (llen > Integer.MAX_VALUE) {
            // should never occur but...
            log.error("Ridiculously huge message payload, above 32-bit length");
        } else {
            final int len = (int) llen;
            final byte[] data = new byte[len];
            final int readLen;
            try {
                readLen = byteMessage.readBytes(data);
            } catch (JMSException e) {
                log.warn("Unable to get message bytes", e);
                return;
            }
            if (readLen < len) {
                log.error("Failed to read byte message contents; read {}, was trying to read {}", readLen, data.length);
            } else {
                final Map event;
                try {
                    event = mapper.readValue(data, Map.class);
                    esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
                } catch (IOException e) {
                    log.error("Failed to convert message to Esper Event", readLen, data.length);
                }
            }
        }
    } else {
        log.error("Unexpected message type '{}' from AMQ broker: must skip", message.getClass().getName());
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Aggregations

TextMessage (javax.jms.TextMessage)191 Test (org.junit.Test)88 Message (javax.jms.Message)63 Session (javax.jms.Session)62 MessageProducer (javax.jms.MessageProducer)59 JMSException (javax.jms.JMSException)47 MessageConsumer (javax.jms.MessageConsumer)37 Destination (javax.jms.Destination)36 Connection (javax.jms.Connection)31 ObjectMessage (javax.jms.ObjectMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)18 StubTextMessage (org.springframework.jms.StubTextMessage)18 BytesMessage (javax.jms.BytesMessage)17 ConnectionFactory (javax.jms.ConnectionFactory)11 QueueConnection (javax.jms.QueueConnection)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 JMSContext (javax.jms.JMSContext)9 MapMessage (javax.jms.MapMessage)9 NamingException (javax.naming.NamingException)8