Search in sources :

Example 1 with MessageProducer

use of javax.jms.MessageProducer 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 2 with MessageProducer

use of javax.jms.MessageProducer 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 3 with MessageProducer

use of javax.jms.MessageProducer 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 4 with MessageProducer

use of javax.jms.MessageProducer in project oxAuth by GluuFederation.

the class ApplicationAuditLogger method loggingThroughJMS.

private void loggingThroughJMS(OAuth2AuditLog oAuth2AuditLog) {
    QueueConnection connection = null;
    try {
        connection = pooledConnectionFactory.createQueueConnection();
        connection.start();
        QueueSession session = connection.createQueueSession(transacted, ACK_MODE);
        MessageProducer producer = session.createProducer(session.createQueue(CLIENT_QUEUE_NAME));
        TextMessage txtMessage = session.createTextMessage();
        txtMessage.setText(ServerUtil.asPrettyJson(oAuth2AuditLog));
        producer.send(txtMessage);
    } catch (JMSException e) {
        log.error("Can't send message", e);
    } catch (IOException e) {
        log.error("Can't serialize the audit log", e);
    } catch (Exception e) {
        log.error("Can't send message, please check your activeMQ configuration.", e);
    } finally {
        if (connection == null)
            return;
        try {
            connection.close();
        } catch (JMSException e) {
            log.error("Can't close connection.");
        }
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) IOException(java.io.IOException) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 5 with MessageProducer

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

the class TopicExportProcessor method sendJMSMessage.

private void sendJMSMessage(String host, int port, String msg, String protocol, String topicName, String clientID, String userName, String password, int timeToLive, boolean isDeliveryModePersistent) throws JMSException {
    // Create a ConnectionFactory
    // network protocol (tcp, ...) set as EXP_ProcessorParameter
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(protocol + "://" + host + ":" + port);
    Connection connection = null;
    Session session = null;
    try {
        // Create a Connection
        if (userName != null && password != null) {
            connection = connectionFactory.createConnection(userName, password);
        } else {
            connection = connectionFactory.createConnection();
        }
        // connection.setClientID( clientID ); Commented by Victor as he had issue!
        connection.start();
        // Create a Session
        //TODO - Trifon could be EXP_ProcessorParameter
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        Destination destination = session.createTopic(topicName);
        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        // EXP_ProcessorParameter
        producer.setTimeToLive(timeToLive);
        if (isDeliveryModePersistent) {
            // EXP_ProcessorParameter	
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            // EXP_ProcessorParameter
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        // How to send to multiple destinations.
        //MessageProducer producer = session.createProducer(null);
        //producer.send(someDestination, message);
        //producer.send(anotherDestination, message);
        // Create a message
        TextMessage message = session.createTextMessage(msg);
        // Tell the producer to send the message
        try {
            producer.send(message);
            session.commit();
            log.info("JMS Message sent!");
        } catch (JMSException ex) {
            session.rollback();
            log.info("JMS Can't send the message!");
            throw ex;
        }
    } finally {
        // Clean up
        if (session != null) {
            try {
                session.close();
            } catch (JMSException ex) {
            /* ignored */
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
            /* ignored */
            }
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

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