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.apache.falcon.oozie.workflow.FalconPostProcessingTest.java

private void consumer(String brokerUrl, String topic, boolean checkUserMessage) throws JMSException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createTopic(topic);
    MessageConsumer consumer = session.createConsumer(destination);

    latch.countDown();/*from   w w w  .  j  av  a 2s. c o  m*/

    // Verify user message
    if (checkUserMessage) {
        verifyMesssage(consumer);
    }

    // Verify falcon message
    verifyMesssage(consumer);

    connection.close();
}

From source file:com.linagora.obm.sync.TestQueueManager.java

@Test
public void testTwoConsumers() throws Exception {
    String testText = "test text";

    Connection connection = createManagedConnection();
    connection.start();
    Session consumerSession1 = createManagedSession(connection);
    MessageConsumer consumer1 = queueManager.createConsumerOnTopic(consumerSession1, TOPIC);

    Session consumerSession2 = createManagedSession(connection);
    MessageConsumer consumer2 = queueManager.createConsumerOnTopic(consumerSession2, TOPIC);

    writeMessageOnTopic(testText, TOPIC, queueManager);

    TextMessage messageReceived1 = (TextMessage) consumer1.receive(TIMEOUT);
    TextMessage messageReceived2 = (TextMessage) consumer2.receive(TIMEOUT);
    Assert.assertEquals(testText, messageReceived1.getText());
    Assert.assertEquals(testText, messageReceived2.getText());
}

From source file:org.apache.activemq.network.MQTTNetworkOfBrokersFailoverTest.java

private CountDownLatch listenForConsumersOn(BrokerService broker) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);

    URI brokerUri = broker.getVmConnectorURI();

    final ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUri.toASCIIString());
    final Connection connection = cf.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session//from   ww  w  .j av a  2s  .co m
            .createTopic("ActiveMQ.Advisory.Consumer.Queue.Consumer.foo:AT_LEAST_ONCE.VirtualTopic.foo.bar");
    MessageConsumer consumer = session.createConsumer(dest);
    consumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            latch.countDown();
            // shutdown this connection
            Dispatch.getGlobalQueue().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        session.close();
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });

    return latch;
}

From source file:com.linagora.obm.sync.TestQueueManager.java

@Test
public void testClosedSession() throws Exception {
    String testText = "test text";

    Connection connection = createManagedConnection();
    connection.start();
    Session consumerSession = queueManager.createSession(connection);
    MessageConsumer consumer = queueManager.createConsumerOnTopic(consumerSession, TOPIC);

    writeMessageOnTopic(testText, TOPIC, queueManager);

    TextMessage messageReceived1 = (TextMessage) consumer.receive(TIMEOUT);
    consumerSession.close();//  w ww  .j  av  a 2  s .co m

    writeMessageOnTopic(testText, TOPIC, queueManager);
    consumerSession = createManagedSession(connection);
    consumer = queueManager.createConsumerOnTopic(consumerSession, TOPIC);

    TextMessage messageReceived2 = (TextMessage) consumer.receive(TIMEOUT);
    Assert.assertEquals(testText, messageReceived1.getText());
    Assert.assertNull(messageReceived2);
}

From source file:de.klemp.middleware.controller.Controller.java

/**
 * This method creates a connection to the message broker Active MQ and
 * sends the message to the given topic. The method is used by all methods
 * of the second component./*w  w w .  ja v a2  s.c o  m*/
 * 
 * @param message
 *            send to the output device
 * @param topic
 *            of the message broker
 */
public static void sendMessage(String message, String topic) {
    String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

    // Create a Connection
    Connection connection;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        Destination destination = session.createTopic(topic);
        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // Create a messages
        TextMessage message1 = session.createTextMessage(message);
        // Tell the producer to send the message
        producer.send(message1);
        session.close();
        connection.close();
    } catch (JMSException e) {
        logger.error("Message could not be sended to activemq", e);
    }

}

From source file:com.linagora.obm.sync.TestQueueManager.java

@Test
public void testDurableSubscription2() throws Exception {
    String testText = "test text";
    String clientId = "c1";

    Connection connection = createManagedConnection();
    connection.setClientID(clientId);/* w w w .  j a va 2 s . c  om*/
    connection.start();
    Session consumerSession = createManagedSession(connection);
    MessageConsumer consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);

    writeMessageOnTopic(testText, TOPIC, queueManager);
    shutdownQueueManager();
    startQueueManager();

    Connection connection2 = createManagedConnection();
    connection2.setClientID(clientId);
    connection2.start();

    consumerSession = createManagedSession(connection2);
    consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);

    TextMessage messageReceived = (TextMessage) consumer.receive(TIMEOUT);
    Assert.assertEquals(testText, messageReceived.getText());
}

From source file:org.ct.amq.jdbc.security.JdbcAuthenticationPluginTest.java

public void testTempDestinations() throws Exception {
    Connection conn = factory.createConnection("guest", "password");
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String name = "org.apache.activemq:BrokerName=localhost,Type=TempTopic";
    try {//from w ww . ja  va2s .c  o  m
        conn.start();
        TemporaryTopic temp = sess.createTemporaryTopic();
        name += ",Destination=" + temp.getTopicName().replaceAll(":", "_");
        fail("Should have failed creating a temp topic");
    } catch (Exception ignore) {
    }

    ObjectName objName = new ObjectName(name);
    TopicViewMBean mbean = (TopicViewMBean) broker.getManagementContext().newProxyInstance(objName,
            TopicViewMBean.class, true);
    try {
        System.out.println(mbean.getName());
        fail("Shouldn't have created a temp topic");
    } catch (Exception ignore) {
    }
}

From source file:com.linagora.obm.sync.TestQueueManager.java

@Test
public void testDurableSubscription() throws Exception {
    String testText = "test text";
    String clientId = "c1";

    Connection connection = queueManager.createConnection();
    connection.setClientID(clientId);//from  www. j  a va2s.c om
    connection.start();
    Session consumerSession = queueManager.createSession(connection);
    MessageConsumer consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);
    consumerSession.close();
    connection.close();

    writeMessageOnTopic(testText, TOPIC, queueManager);

    Connection connection2 = createManagedConnection();
    connection2.setClientID(clientId);
    connection2.start();

    consumerSession = queueManager.createSession(connection2);
    consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);

    TextMessage messageReceived = (TextMessage) consumer.receive(TIMEOUT);
    Assert.assertEquals(testText, messageReceived.getText());
}

From source file:org.apache.falcon.messaging.JMSMessageConsumerTest.java

public void sendMessages(String topic, WorkflowExecutionContext.Type type, boolean isFalconWF)
        throws JMSException, FalconException, IOException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic destination = session.createTopic(topic);
    javax.jms.MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

    for (int i = 0; i < 5; i++) {
        Message message = null;/*from w w w .j  a va2s.  c o m*/

        switch (type) {
        case POST_PROCESSING:
            message = getMockFalconMessage(i, session);
            break;
        case WORKFLOW_JOB:
            message = getMockOozieMessage(i, session, isFalconWF);
            break;
        case COORDINATOR_ACTION:
            message = getMockOozieCoordMessage(i, session, isFalconWF);
        default:
            break;
        }
        Log.debug("Sending:" + message);
        producer.send(message);
    }
}

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;//from   www  . j a  va 2s.  c  o  m
    Destination orderQueue;
    try {
        Connection connection = connectionFactory.createConnection();

        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        orderQueue = session.createQueue(QUEUE);
        MessageConsumer consumer = session.createConsumer(orderQueue);

        connection.start();

        while (true) {
            Message message = consumer.receive();
            MessageProducer producer = session.createProducer(message.getJMSReplyTo());
            MapMessage orderMessage;
            if (message instanceof MapMessage) {
                orderMessage = (MapMessage) message;
            } else {
                // End of Stream
                producer.send(session.createMessage());
                session.commit();
                producer.close();
                break;
            }

            int quantity = orderMessage.getInt("Quantity");
            System.out.println(
                    ITEM + " Supplier: Vendor ordered " + quantity + " " + orderMessage.getString("Item"));

            MapMessage outMessage = session.createMapMessage();
            outMessage.setInt("VendorOrderNumber", orderMessage.getInt("VendorOrderNumber"));
            outMessage.setString("Item", ITEM);

            quantity = Math.min(orderMessage.getInt("Quantity"),
                    new Random().nextInt(orderMessage.getInt("Quantity") * 10));
            outMessage.setInt("Quantity", quantity);

            producer.send(outMessage);
            System.out.println(ITEM + " Supplier: Sent " + quantity + " " + ITEM + "(s)");
            session.commit();
            System.out.println(ITEM + " Supplier: committed transaction");
            producer.close();
        }
        connection.close();
    } catch (JMSException e) {
        e.printStackTrace();
    }
}