Search in sources :

Example 31 with MessageProducer

use of javax.jms.MessageProducer in project wildfly-camel by wildfly-extras.

the class JMSIntegrationTest method testMessageConsumerRouteWithClientAck.

@Test
public void testMessageConsumerRouteWithClientAck() throws Exception {
    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(3);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            fromF("jms:queue:%s?connectionFactory=ConnectionFactory&acknowledgementModeName=CLIENT_ACKNOWLEDGE", QUEUE_NAME).process(exchange -> {
                JmsMessage in = (JmsMessage) exchange.getIn();
                Session session = in.getJmsSession();
                TextMessage message = (TextMessage) in.getJmsMessage();
                long count = latch.getCount();
                try {
                    // always append the message text
                    result.add(message.getText() + " " + (4 - count));
                    if (count == 3) {
                    // do nothing on first
                    // note, this message is still acked because of
                    // [CAMEL-8749] JMS message always acknowledged even with CLIENT_ACKNOWLEDGE
                    } else if (count == 2) {
                        // recover causing a redelivery
                        session.recover();
                    } else {
                        // acknowledge
                        message.acknowledge();
                    }
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            }).transform(simple("Hello ${body}")).to("seda:end");
        }
    });
    camelctx.start();
    PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
    consumer.start();
    try {
        // Send a message to the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        connection.start();
        Destination destination = (Destination) initialctx.lookup(QUEUE_JNDI_NAME);
        MessageProducer producer = session.createProducer(destination);
        try {
            producer.send(session.createTextMessage("Kermit"));
            producer.send(session.createTextMessage("Piggy"));
            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("Four messages", 3, result.size());
            Assert.assertEquals("Kermit 1", result.get(0));
            Assert.assertEquals("Piggy 2", result.get(1));
            Assert.assertEquals("Piggy 3", result.get(2));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ServerSetupTask(org.jboss.as.arquillian.api.ServerSetupTask) Arquillian(org.jboss.arquillian.junit.Arquillian) PollingConsumer(org.apache.camel.PollingConsumer) ServerSetup(org.jboss.as.arquillian.api.ServerSetup) RunWith(org.junit.runner.RunWith) MessageListener(javax.jms.MessageListener) JMSUtils(org.wildfly.camel.test.common.utils.JMSUtils) ArrayList(java.util.ArrayList) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) CamelAware(org.wildfly.extension.camel.CamelAware) ArquillianResource(org.jboss.arquillian.test.api.ArquillianResource) ProducerTemplate(org.apache.camel.ProducerTemplate) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) MessageProducer(javax.jms.MessageProducer) Message(javax.jms.Message) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) JmsMessage(org.apache.camel.component.jms.JmsMessage) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) CamelContext(org.apache.camel.CamelContext) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) TextMessage(javax.jms.TextMessage) Test(org.junit.Test) JMSException(javax.jms.JMSException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) Destination(javax.jms.Destination) RouteBuilder(org.apache.camel.builder.RouteBuilder) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) MessageConverter(org.springframework.jms.support.converter.MessageConverter) ManagementClient(org.jboss.as.arquillian.container.ManagementClient) Assert(org.junit.Assert) ConnectionFactory(javax.jms.ConnectionFactory) Destination(javax.jms.Destination) PollingConsumer(org.apache.camel.PollingConsumer) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) JmsMessage(org.apache.camel.component.jms.JmsMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 32 with MessageProducer

use of javax.jms.MessageProducer in project wildfly-camel by wildfly-extras.

the class SJMSIntegrationTest method sendMessage.

private void sendMessage(Connection connection, String jndiName, String message) throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = (Destination) initialctx.lookup(jndiName);
    MessageProducer producer = session.createProducer(destination);
    TextMessage msg = session.createTextMessage(message);
    producer.send(msg);
    connection.start();
}
Also used : Destination(javax.jms.Destination) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 33 with MessageProducer

use of javax.jms.MessageProducer in project UVMS-Docker by UnionVMS.

the class MessageHelper method sendMessage.

public static void sendMessage(String queueName, final String msg) throws Exception {
    String responseQueueName = queueName + "Response" + UUID.randomUUID().toString().replaceAll("-", "");
    Connection connection = connectionFactory.createConnection();
    connection.setClientID(UUID.randomUUID().toString());
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Queue queue = session.createQueue(queueName);
    final MessageProducer messageProducer = session.createProducer(queue);
    messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
    messageProducer.setTimeToLive(1000000000);
    final Queue responseQueue = session.createQueue(responseQueueName);
    TextMessage createTextMessage = session.createTextMessage(msg);
    createTextMessage.setJMSReplyTo(responseQueue);
    messageProducer.send(createTextMessage);
    session.close();
    connection.close();
}
Also used : Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 34 with MessageProducer

use of javax.jms.MessageProducer in project wildfly-camel by wildfly-extras.

the class ActiveMQIntegrationTest method sendMessage.

private void sendMessage(Connection connection, String message) throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(QUEUE_NAME);
    MessageProducer producer = session.createProducer(destination);
    TextMessage msg = session.createTextMessage(message);
    producer.send(msg);
    connection.start();
}
Also used : Destination(javax.jms.Destination) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 35 with MessageProducer

use of javax.jms.MessageProducer in project tesb-rt-se by Talend.

the class JMSHttpBookStore method postOneWayBook.

private void postOneWayBook(Session session, Destination destination, Book book) throws Exception {
    MessageProducer producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage();
    message.setText(writeBook(book));
    producer.send(message);
    producer.close();
}
Also used : MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage)

Aggregations

MessageProducer (javax.jms.MessageProducer)1512 Session (javax.jms.Session)1302 MessageConsumer (javax.jms.MessageConsumer)987 Connection (javax.jms.Connection)915 TextMessage (javax.jms.TextMessage)888 Test (org.junit.Test)850 Message (javax.jms.Message)646 Queue (javax.jms.Queue)404 JMSException (javax.jms.JMSException)213 Destination (javax.jms.Destination)190 ConnectionFactory (javax.jms.ConnectionFactory)147 BytesMessage (javax.jms.BytesMessage)140 Topic (javax.jms.Topic)140 TopicSession (javax.jms.TopicSession)135 TopicConnection (javax.jms.TopicConnection)115 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)108 ObjectMessage (javax.jms.ObjectMessage)98 CountDownLatch (java.util.concurrent.CountDownLatch)91 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)90 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)88