Search in sources :

Example 36 with Connection

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

the class JCAInflowBindingTest method triggerGreetingService.

@Test
public void triggerGreetingService() throws Exception {
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_destination);
        TextMessage message = session.createTextMessage();
        message.setText(PAYLOAD);
        producer.send(message);
        session.close();
        _logger.info("Sent a message into " + _destination.toString() + " - following message should appear on server console:");
        _logger.info("Hello there dummy :-)");
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_destination);
        conn.start();
        Assert.assertNull("Request message is still in the queue: " + _destination.toString(), consumer.receive(3000));
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 37 with Connection

use of javax.jms.Connection 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)

Example 38 with Connection

use of javax.jms.Connection in project beam by apache.

the class JmsIOTest method testWriteMessage.

@Test
public void testWriteMessage() throws Exception {
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        data.add("Message " + i);
    }
    pipeline.apply(Create.of(data)).apply(JmsIO.write().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD));
    pipeline.run();
    Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
    int count = 0;
    while (consumer.receive(1000) != null) {
        count++;
    }
    assertEquals(100, count);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) Matchers.containsString(org.hamcrest.Matchers.containsString) Session(javax.jms.Session) Test(org.junit.Test)

Example 39 with Connection

use of javax.jms.Connection in project beam by apache.

the class JmsIOTest method testCheckpointMark.

@Test
public void testCheckpointMark() throws Exception {
    // we are using no prefetch here
    // prefetch is an ActiveMQ feature: to make efficient use of network resources the broker
    // utilizes a 'push' model to dispatch messages to consumers. However, in the case of our
    // test, it means that we can have some latency between the receiveNoWait() method used by
    // the consumer and the prefetch buffer populated by the broker. Using a prefetch to 0 means
    // that the consumer will poll for message, which is exactly what we want for the test.
    Connection connection = connectionFactoryWithoutPrefetch.createConnection(USERNAME, PASSWORD);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
    for (int i = 0; i < 10; i++) {
        producer.send(session.createTextMessage("test " + i));
    }
    producer.close();
    session.close();
    connection.close();
    JmsIO.Read spec = JmsIO.read().withConnectionFactory(connectionFactoryWithoutPrefetch).withUsername(USERNAME).withPassword(PASSWORD).withQueue(QUEUE);
    JmsIO.UnboundedJmsSource source = new JmsIO.UnboundedJmsSource(spec);
    JmsIO.UnboundedJmsReader reader = source.createReader(null, null);
    // start the reader and move to the first record
    assertTrue(reader.start());
    // consume 3 messages (NB: start already consumed the first message)
    for (int i = 0; i < 3; i++) {
        assertTrue(reader.advance());
    }
    // the messages are still pending in the queue (no ACK yet)
    assertEquals(10, count(QUEUE));
    // we finalize the checkpoint
    reader.getCheckpointMark().finalizeCheckpoint();
    // the checkpoint finalize ack the messages, and so they are not pending in the queue anymore
    assertEquals(6, count(QUEUE));
    // we read the 6 pending messages
    for (int i = 0; i < 6; i++) {
        assertTrue(reader.advance());
    }
    // still 6 pending messages as we didn't finalize the checkpoint
    assertEquals(6, count(QUEUE));
    // we finalize the checkpoint: no more message in the queue
    reader.getCheckpointMark().finalizeCheckpoint();
    assertEquals(0, count(QUEUE));
}
Also used : Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 40 with Connection

use of javax.jms.Connection in project beam by apache.

the class JmsIOTest method testReadMessages.

@Test
public void testReadMessages() throws Exception {
    // produce message
    Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
    TextMessage message = session.createTextMessage("This Is A Test");
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.close();
    session.close();
    connection.close();
    // read from the queue
    PCollection<JmsRecord> output = pipeline.apply(JmsIO.read().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD).withMaxNumRecords(5));
    PAssert.thatSingleton(output.apply("Count", Count.<JmsRecord>globally())).isEqualTo(new Long(5));
    pipeline.run();
    connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
    Message msg = consumer.receiveNoWait();
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

Connection (javax.jms.Connection)124 Session (javax.jms.Session)88 Test (org.junit.Test)62 MessageProducer (javax.jms.MessageProducer)46 ConnectionFactory (javax.jms.ConnectionFactory)44 Message (javax.jms.Message)38 MessageConsumer (javax.jms.MessageConsumer)38 JMSException (javax.jms.JMSException)32 TextMessage (javax.jms.TextMessage)32 QueueConnection (javax.jms.QueueConnection)22 TopicConnection (javax.jms.TopicConnection)22 Destination (javax.jms.Destination)15 Queue (javax.jms.Queue)12 QueueConnectionFactory (javax.jms.QueueConnectionFactory)11 TopicConnectionFactory (javax.jms.TopicConnectionFactory)11 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)7 TransactionAttribute (javax.ejb.TransactionAttribute)4