Search in sources :

Example 26 with MessageConsumer

use of javax.jms.MessageConsumer in project wildfly by wildfly.

the class JmsClientTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup) throws Exception {
    Connection conn = null;
    try {
        ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
        assertNotNull(cf);
        Destination destination = (Destination) remoteContext.lookup(QUEUE_NAME);
        assertNotNull(destination);
        conn = cf.createConnection("guest", "guest");
        conn.start();
        Session consumerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        // Set the async listener
        MessageConsumer consumer = consumerSession.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                TextMessage msg = (TextMessage) message;
                try {
                    result.add(msg.getText());
                    latch.countDown();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        final Session producerSession = conn.createSession(false, AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSession.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 10; i++) {
            String s = "Test" + i;
            TextMessage msg = producerSession.createTextMessage(s);
            //System.out.println("sending " + s);
            producer.send(msg);
        }
        producerSession.close();
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    } finally {
        try {
            conn.close();
        } catch (Exception ignore) {
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 27 with MessageConsumer

use of javax.jms.MessageConsumer in project adempiere by adempiere.

the class TopicListener method run.

public void run() throws JMSException {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    log.finest("ActiveMQConnectionFactory = " + factory);
    if (userName != null && password != null) {
        conn = factory.createConnection(userName, password);
    } else {
        conn = factory.createConnection();
    }
    log.finest("conn = " + conn);
    if (conn.getClientID() == null) {
        try {
            conn.setClientID(clientID);
        } catch (Exception e) {
            //log.info("Connection with clientID '" + clientID +"' already exists" + e.toString());
            conn.close();
            return;
        }
    } else {
        if (conn.getClientID().equals(clientID)) {
            log.warning("Connection with clientID '" + clientID + "' already exists");
            conn.close();
            return;
        } else {
            try {
                conn.setClientID(clientID);
            } catch (Exception e) {
                log.info("Error while invoking setClientID(" + clientID + ")! " + e.getMessage());
                conn.close();
                return;
            }
        }
    }
    // TODO - could be parameter
    session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
    log.finest("session = " + session);
    log.finest("topicName = " + topicName);
    log.finest("subscriptionName = " + subscriptionName);
    topic = session.createTopic(topicName);
    log.finest("topic = " + topic);
    MessageConsumer consumer = null;
    if (isDurableSubscription) {
        // http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.pmc.express.doc/tasks/tjn0012_.html
        // The subscriptionName assigned to a durable subscription must be unique within a given client ID.
        consumer = session.createDurableSubscriber(topic, subscriptionName);
    } else {
        consumer = session.createConsumer(topic);
    }
    log.finest("consumer = " + consumer);
    consumer.setMessageListener(this);
    conn.start();
    log.finest("Waiting for JMS messages...");
    if (replicationProcessor != null) {
        MIMPProcessorLog pLog = new MIMPProcessorLog(replicationProcessor.getMImportProcessor(), "Connected to JMS Server. Waiting for messages!");
        StringBuffer logReference = new StringBuffer("topicName = ").append(topicName).append(", subscriptionName = ").append(subscriptionName);
        pLog.setReference(logReference.toString());
        boolean resultSave = pLog.save();
        log.finest("Result Save = " + resultSave);
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) MIMPProcessorLog(org.compiere.model.MIMPProcessorLog) JMSException(javax.jms.JMSException)

Example 28 with MessageConsumer

use of javax.jms.MessageConsumer in project ignite by apache.

the class JmsStreamer method initializeJmsObjectsForQueue.

private void initializeJmsObjectsForQueue() throws JMSException {
    for (int i = 0; i < threads; i++) {
        Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
        if (destination == null)
            destination = session.createQueue(destinationName);
        MessageConsumer consumer = session.createConsumer(destination);
        IgniteJmsMessageListener messageListener = new IgniteJmsMessageListener(session, false);
        consumer.setMessageListener(messageListener);
        consumers.add(consumer);
        sessions.add(session);
        listeners.add(messageListener);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Session(javax.jms.Session)

Example 29 with MessageConsumer

use of javax.jms.MessageConsumer in project logging-log4j2 by apache.

the class JmsAppenderIT method testLogMapMessageToQueue.

@Test
public void testLogMapMessageToQueue() throws Exception {
    setUp(MessageLayout.createLayout());
    final int messageCount = 100;
    final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
    final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount, javax.jms.MapMessage.class);
    messageConsumer.setMessageListener(consumer);
    final String messageText = "Hello, World!";
    final String loggerName = this.getClass().getName();
    for (int i = 0; i < messageCount; i++) {
        Map<String, String> map = new HashMap<>();
        map.put("messageText", messageText);
        map.put("threadName", Thread.currentThread().getName());
        final LogEvent event = //
        Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerName).setLevel(//
        Level.INFO).setMessage(//
        new MapMessage(map)).setTimeMillis(System.currentTimeMillis()).build();
        appender.append(event);
    }
    consumer.awaitAndAssertAllMessagesConsumed();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) Test(org.junit.Test)

Example 30 with MessageConsumer

use of javax.jms.MessageConsumer in project logging-log4j2 by apache.

the class JmsAppenderIT method testLogObjectMessageToQueue.

@Test
public void testLogObjectMessageToQueue() throws Exception {
    setUp(SerializedLayout.createLayout());
    final int messageCount = 100;
    final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
    final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount, ObjectMessage.class);
    messageConsumer.setMessageListener(consumer);
    final String messageText = "Hello, World!";
    final String loggerName = this.getClass().getName();
    for (int i = 0; i < messageCount; i++) {
        final LogEvent event = //
        Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerName).setLevel(//
        Level.INFO).setMessage(new SimpleMessage(messageText)).setThreadName(//
        Thread.currentThread().getName()).setTimeMillis(System.currentTimeMillis()).build();
        appender.append(event);
    }
    consumer.awaitAndAssertAllMessagesConsumed();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3