Search in sources :

Example 71 with Connection

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

the class NotificationListener method createConnection.

/**
   * Create the JMS connection
   * @return newly created JMS connection
   */
protected Connection createConnection() {
    LOG.info("Will create new JMS connection");
    Context jndiCntxt;
    Connection jmsConnection = null;
    try {
        jndiCntxt = new InitialContext();
        ConnectionFactory connFac = (ConnectionFactory) jndiCntxt.lookup("ConnectionFactory");
        jmsConnection = connFac.createConnection();
        jmsConnection.start();
        jmsConnection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException jmse) {
                LOG.error("JMS Exception listener received exception. Ignored the error", jmse);
            }
        });
    } catch (NamingException e) {
        LOG.error("JNDI error while setting up Message Bus connection. " + "Please make sure file named 'jndi.properties' is in " + "classpath and contains appropriate key-value pairs.", e);
    } catch (JMSException e) {
        LOG.error("Failed to initialize connection to message bus", e);
    } catch (Throwable t) {
        LOG.error("Unable to connect to JMS provider", t);
    }
    return jmsConnection;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 72 with Connection

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

the class JMSClient method sendToActiveMQ.

private static void sendToActiveMQ(String payload, String queueName) throws Exception {
    ConnectionFactory cf = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASSWD, AMQ_BROKER_URL);
    Connection conn = cf.createConnection();
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(session.createQueue(queueName));
        producer.send(session.createTextMessage(payload));
        session.close();
        verifyOutputQueue(conn.createSession(false, Session.AUTO_ACKNOWLEDGE));
    } finally {
        conn.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 73 with Connection

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

the class JmsBindingTest method testRollbackA.

/**
     * Triggers the 'WorkService' by sending a HornetQ Message to the 'policyQSTransacted' queue.
     */
@Test
public void testRollbackA() throws Exception {
    String command = "rollback.A";
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_queueIn);
        TextMessage message = session.createTextMessage();
        message.setText(command);
        producer.send(message);
        session.close();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_queueOutA);
        TextMessage msg = TextMessage.class.cast(consumer.receive(30000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutB);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutC);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        session.close();
    } 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 74 with Connection

use of javax.jms.Connection in project javaee7-samples by javaee-samples.

the class ClassicMessageSender method sendMessage.

public void sendMessage(String payload) {
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer = session.createProducer(demoQueue);
        TextMessage textMessage = session.createTextMessage(payload);
        messageProducer.send(textMessage);
    } catch (JMSException ex) {
        ex.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
}
Also used : Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 75 with Connection

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

the class ConnectionResourceIT method testCreateConnections.

/**
     * Test method for
     * {@link org.apache.commons.pool.ObjectPool#returnObject(java.lang.Object)}
     * .
     * 
     * @throws Exception
     */
@Test
public void testCreateConnections() throws Exception {
    ConnectionResource pool = new AMQConnectionResource("tcp://localhost:33333", 1);
    assertNotNull(pool);
    Connection connection = pool.borrowConnection();
    assertNotNull(connection);
    assertNotNull(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
    pool.returnConnection(connection);
    Connection connection2 = pool.borrowConnection();
    assertNotNull(connection2);
}
Also used : ConnectionResource(org.apache.camel.component.sjms.jms.ConnectionResource) Connection(javax.jms.Connection) 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