Search in sources :

Example 76 with MessageListener

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

the class LegacyJMSTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
    Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
    assertNotNull(destination);
    ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
    assertNotNull(cf);
    try (JMSContext producerContext = cf.createContext("guest", "guest");
        JMSContext consumerContext = cf.createContext("guest", "guest")) {
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        JMSConsumer consumer = consumerContext.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();
                }
            }
        });
        JMSProducer producer = producerContext.createProducer();
        for (int i = 0; i < 10; i++) {
            String text = "Test" + i;
            producer.send(destination, text);
        }
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    }
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 77 with MessageListener

use of javax.jms.MessageListener in project ats-framework by Axway.

the class JmsClient method startListeningToTopic.

/**
 * Start listening for messages on topic
 *
 * @param topicName the topic name
 */
@PublicAtsApi
public void startListeningToTopic(final String topicName) {
    final TopicInfo topicInfo = getTopicInfo(topicName);
    if (topicInfo.isListening()) {
        throw new JmsMessageException("We are already listening for messages on topic " + topicName);
    }
    try {
        final Session session = loadSession(false, Session.AUTO_ACKNOWLEDGE);
        final Topic topic = session.createTopic(topicName);
        topicInfo.topicConsumer = session.createConsumer(topic);
        topicInfo.topicConsumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                topicInfo.addMessage(message);
            }
        });
    } catch (JMSException e) {
        throw new JmsMessageException("Could not start listening for messages on topic " + topicName, e);
    }
}
Also used : JmsMessageException(com.axway.ats.action.exceptions.JmsMessageException) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) Session(javax.jms.Session) ManagedSession(com.axway.ats.action.jms.model.sessions.ManagedSession) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 78 with MessageListener

use of javax.jms.MessageListener in project tech by ffyyhh995511.

the class ActiveMqService method receiveTopic.

/**
 * 接受active mq的topic(订阅)
 * 通过一个消费者的监听接受推送的消息
 * @param topicStr
 * @throws Exception
 */
public void receiveTopic(String topicStr) throws Exception {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicStr);
    MessageConsumer consumer = session.createConsumer(topic);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            TextMessage tm = (TextMessage) message;
            System.out.println(tm);
            try {
                System.out.println(tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 79 with MessageListener

use of javax.jms.MessageListener in project spring-framework by spring-projects.

the class AbstractJmsListenerEndpoint method setupMessageListener.

private void setupMessageListener(MessageListenerContainer container) {
    MessageListener messageListener = createMessageListener(container);
    if (messageListener == null) {
        throw new IllegalStateException("Endpoint [" + this + "] must provide a non-null message listener");
    }
    container.setupMessageListener(messageListener);
}
Also used : MessageListener(javax.jms.MessageListener)

Example 80 with MessageListener

use of javax.jms.MessageListener in project qpid-broker-j by apache.

the class ClientJmsDelegate method setInstructionListener.

public void setInstructionListener(final Client client) {
    try {
        _instructionQueue = _instructionListenerSession.createTemporaryQueue();
        final MessageConsumer instructionConsumer = _instructionListenerSession.createConsumer(_instructionQueue);
        instructionConsumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(final Message message) {
                client.processInstruction(JmsMessageAdaptor.messageToCommand(message));
            }
        });
    } catch (final JMSException jmse) {
        throw new DistributedTestException("Unable to setup instruction listener", jmse);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) DistributedTestException(org.apache.qpid.disttest.DistributedTestException) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException)

Aggregations

MessageListener (javax.jms.MessageListener)146 Message (javax.jms.Message)128 MessageConsumer (javax.jms.MessageConsumer)103 Session (javax.jms.Session)97 TextMessage (javax.jms.TextMessage)95 CountDownLatch (java.util.concurrent.CountDownLatch)72 Test (org.junit.Test)71 Connection (javax.jms.Connection)60 MessageProducer (javax.jms.MessageProducer)58 JMSException (javax.jms.JMSException)57 Destination (javax.jms.Destination)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)26 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)16 ScheduledMessage (org.apache.activemq.ScheduledMessage)15 ArrayList (java.util.ArrayList)14 Topic (javax.jms.Topic)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)13