Search in sources :

Example 1 with Connection

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

the class JmsProducer method testConnectionOnStartup.

/**
     * Pre tests the connection before starting the listening.
     * <p/>
     * In case of connection failure the exception is thrown which prevents Camel from starting.
     *
     * @throws FailedToCreateProducerException is thrown if testing the connection failed
     */
protected void testConnectionOnStartup() throws FailedToCreateProducerException {
    try {
        CamelJmsTemplate template = (CamelJmsTemplate) getInOnlyTemplate();
        if (log.isDebugEnabled()) {
            log.debug("Testing JMS Connection on startup for destination: " + template.getDefaultDestinationName());
        }
        Connection conn = template.getConnectionFactory().createConnection();
        JmsUtils.closeConnection(conn);
        log.debug("Successfully tested JMS Connection on startup for destination: " + template.getDefaultDestinationName());
    } catch (Exception e) {
        throw new FailedToCreateProducerException(getEndpoint(), e);
    }
}
Also used : FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) CamelJmsTemplate(org.apache.camel.component.jms.JmsConfiguration.CamelJmsTemplate) Connection(javax.jms.Connection) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JMSException(javax.jms.JMSException) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 2 with Connection

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

the class JmsConsumer method testConnectionOnStartup.

/**
     * Pre tests the connection before starting the listening.
     * <p/>
     * In case of connection failure the exception is thrown which prevents Camel from starting.
     *
     * @throws FailedToCreateConsumerException is thrown if testing the connection failed
     */
protected void testConnectionOnStartup() throws FailedToCreateConsumerException {
    try {
        log.debug("Testing JMS Connection on startup for destination: {}", getDestinationName());
        Connection con = listenerContainer.getConnectionFactory().createConnection();
        JmsUtils.closeConnection(con);
        log.debug("Successfully tested JMS Connection on startup for destination: {}", getDestinationName());
    } catch (Exception e) {
        String msg = "Cannot get JMS Connection on startup for destination " + getDestinationName();
        throw new FailedToCreateConsumerException(getEndpoint(), msg, e);
    }
}
Also used : FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) Connection(javax.jms.Connection) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException)

Example 3 with Connection

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

the class TestMsgBusConnection method connectClient.

private void connectClient() throws JMSException {
    ConnectionFactory connFac = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection conn = connFac.createConnection();
    conn.start();
    Session session = conn.createSession(true, Session.SESSION_TRANSACTED);
    Destination hcatTopic = session.createTopic("planetlab.hcat");
    consumer = session.createConsumer(hcatTopic);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 4 with Connection

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

the class ClassicMessageReceiver method receiveMessage.

public String receiveMessage() {
    String response = null;
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer messageConsumer = session.createConsumer(demoQueue);
        Message message = messageConsumer.receive(5000);
        response = message.getBody(String.class);
    } catch (JMSException ex) {
        ex.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
    return response;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 5 with Connection

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

the class CamelJMSBindingTest method sendTextToQueue.

private void sendTextToQueue(final String text, final String queueName) throws Exception {
    InitialContext initialContext = null;
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;
    try {
        initialContext = new InitialContext();
        final Queue testQueue = (Queue) initialContext.lookup(queueName);
        final ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(testQueue);
        producer.send(session.createTextMessage(text));
    } finally {
        if (producer != null) {
            producer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
        if (initialContext != null) {
            initialContext.close();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) Session(javax.jms.Session)

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