Example usage for javax.jms Connection start

List of usage examples for javax.jms Connection start

Introduction

In this page you can find the example usage for javax.jms Connection start.

Prototype


void start() throws JMSException;

Source Link

Document

Starts (or restarts) a connection's delivery of incoming messages.

Usage

From source file:org.springframework.jms.connection.ConnectionFactoryUtils.java

/**
 * Obtain a JMS Session that is synchronized with the current transaction, if any.
 * @param connectionFactory the JMS ConnectionFactory to bind for
 * (used as TransactionSynchronizationManager key)
 * @param resourceFactory the ResourceFactory to use for extracting or creating
 * JMS resources/*  www.  j av a2 s.  c  o  m*/
 * @param startConnection whether the underlying JMS Connection approach should be
 * started in order to allow for receiving messages. Note that a reused Connection
 * may already have been started before, even if this flag is {@code false}.
 * @return the transactional Session, or {@code null} if none found
 * @throws JMSException in case of JMS failure
 */
@Nullable
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory,
        ResourceFactory resourceFactory, boolean startConnection) throws JMSException {

    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");

    JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    if (resourceHolder != null) {
        Session session = resourceFactory.getSession(resourceHolder);
        if (session != null) {
            if (startConnection) {
                Connection con = resourceFactory.getConnection(resourceHolder);
                if (con != null) {
                    con.start();
                }
            }
            return session;
        }
        if (resourceHolder.isFrozen()) {
            return null;
        }
    }
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        return null;
    }
    JmsResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new JmsResourceHolder(connectionFactory);
    }
    Connection con = resourceFactory.getConnection(resourceHolderToUse);
    Session session = null;
    try {
        boolean isExistingCon = (con != null);
        if (!isExistingCon) {
            con = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(con);
        }
        session = resourceFactory.createSession(con);
        resourceHolderToUse.addSession(session, con);
        if (startConnection) {
            con.start();
        }
    } catch (JMSException ex) {
        if (session != null) {
            try {
                session.close();
            } catch (Throwable ex2) {
                // ignore
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (Throwable ex2) {
                // ignore
            }
        }
        throw ex;
    }
    if (resourceHolderToUse != resourceHolder) {
        TransactionSynchronizationManager.registerSynchronization(new JmsResourceSynchronization(
                resourceHolderToUse, connectionFactory, resourceFactory.isSynchedLocalTransactionAllowed()));
        resourceHolderToUse.setSynchronizedWithTransaction(true);
        TransactionSynchronizationManager.bindResource(connectionFactory, resourceHolderToUse);
    }
    return session;
}

From source file:unic.mentoring.jms.ctrl.MessageCtrl.java

protected void sendMessage(String message, String topicName) throws JMSException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    Connection connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicName);
    MessageProducer producer = session.createProducer(topic);
    TextMessage jmsMessage = session.createTextMessage();
    jmsMessage.setText(message);//from w ww. j  av  a 2s .  co  m

    producer.send(jmsMessage);
    connection.close();
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueReceiveFailTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);

    Message rawMessage = consumer.receive(1000);
    assertNull(rawMessage);//from  w ww .  ja  v  a  2 s  .  c  o m
    connection.close();
}

From source file:com.navercorp.pinpoint.demo.gateway.configuration.GatewayConfiguration.java

@Bean
Connection amqConnection(ConnectionFactory amqConnectionFactory) throws JMSException {
    Connection connection = amqConnectionFactory.createConnection();
    connection.start();
    connection.setExceptionListener(e -> System.out.println("JMS Exception occurred. Shutting down client."));
    return connection;
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);

    Message rawMessage = consumer.receive(1000);
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();/* ww w. j  a v  a2  s.c om*/
}

From source file:sk.seges.test.jms.multiple.QueueSendReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = testConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(testQueueA);
    Message rawMessage = consumer.receive();
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();/*from   ww  w.j  a  va  2s.  c o  m*/
}

From source file:Log4jJMSAppenderExample.java

public Log4jJMSAppenderExample() throws Exception {
    // create a logTopic topic consumer
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection conn = factory.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    conn.start();
    MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic"));
    consumer.setMessageListener(this);
    // log a message
    Logger log = Logger.getLogger(Log4jJMSAppenderExample.class);
    log.info("Test log");
    // clean up//  w w w  .  j a  v  a 2 s.c  o  m
    Thread.sleep(1000);
    consumer.close();
    sess.close();
    conn.close();
    System.exit(1);
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueSendReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);
    Message rawMessage = consumer.receive();
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();/*from   w  ww. j a  v a  2  s  . co  m*/
}

From source file:org.apache.servicemix.samples.bridge.BridgeTest.java

public void testSimpleCall() throws Exception {

    ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Destination outQueue = new ActiveMQQueue("bridge.output");
    Connection connection = factory.createConnection();
    connection.start();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer createConsumer = session.createConsumer(outQueue);
    createConsumer.setMessageListener(new MessageListener() {

        public void onMessage(Message arg0) {
            BridgeTest.recieved = true;// w  ww .  j a v  a2  s .  co m
            ActiveMQTextMessage msg = (ActiveMQTextMessage) arg0;
            assertNotNull(msg);
        }

    });

    HttpClient client = new HttpClient();

    PostMethod post = new PostMethod(url);
    post.setRequestBody(new FileInputStream("src/test/resources/request.xml"));

    int executeMethod = client.executeMethod(post);

    assertEquals(202, executeMethod);

    int maxTry = 100;
    while (!recieved || maxTry == 0) {
        Thread.sleep(200);
        maxTry--;
    }

    session.close();
    connection.close();

}

From source file:org.apache.activemq.store.kahadb.MultiKahaDBTopicDeletionTest.java

@Override
protected void createConsumer(ActiveMQDestination dest) throws JMSException {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerConnectURI);
    Connection connection = factory.createConnection();
    connection.setClientID("client1");
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    session.createDurableSubscriber((Topic) dest, "sub1");
}