Example usage for javax.jms Connection setClientID

List of usage examples for javax.jms Connection setClientID

Introduction

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

Prototype

void setClientID(String clientID) throws JMSException;

Source Link

Document

Sets the client identifier for this connection.

Usage

From source file:example.topic.durable.Subscriber.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();/*w  w w .  j a v a  2 s . c  o m*/
    }
    System.out
            .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;
    final CountDownLatch latch = new CountDownLatch(1);

    try {

        connection = connectionFactory.createConnection();
        String clientId = System.getProperty("clientId");
        connection.setClientID(clientId);

        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Topic destination = session.createTopic("test-topic");

        MessageConsumer consumer = session.createDurableSubscriber(destination, clientId);
        consumer.setMessageListener(new Subscriber(latch));

        latch.await();
        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:com.fusesource.examples.activemq.DurableSubscriber.java

public static void main(String args[]) {
    Connection connection = null;

    try {/*from   ww  w. ja  v a2  s . co  m*/
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);
        connection = factory.createConnection();
        connection.setClientID(System.getProperty("clientID"));
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.CLIENT_ACKNOWLEDGE);
        String topicName = System.getProperty("topic");

        Topic topic = session.createTopic(topicName);
        MessageConsumer consumer = session.createDurableSubscriber(topic, "Test_Durable_Subscriber");

        LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS
                + "ms timeout");

        // Synchronous message consumer
        int i = 1;
        while (true) {
            Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS);
            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    LOG.info("Got " + (i++) + ". message: " + text);
                }
            } else {
                break;
            }
        }

        consumer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:com.fusesource.examples.activemq.VirtualConsumer.java

public static void main(String args[]) {
    Connection connection = null;

    try {/* w  ww  .  j  ava 2  s . c  o m*/
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);

        connection = factory.createConnection();
        connection.setClientID(System.getProperty("clientID"));
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(System.getProperty("queue"));

        MessageConsumer consumer = session.createConsumer(queue);

        LOG.info("Start consuming messages from " + queue + " with " + MESSAGE_TIMEOUT_MILLISECONDS
                + "ms timeout");

        // Synchronous message consumer
        int i = 1;
        while (true) {
            Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS);
            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    LOG.info("Got " + (i++) + ". message: " + text);
                }
            } else {
                break;
            }
        }

        consumer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:com.fusesource.examples.activemq.SimpleSubscriber.java

public static void main(String args[]) {
    Connection connection = null;

    try {// w  w  w .j  av a2 s .  c o  m
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);
        connection = factory.createConnection();
        connection.setClientID("ApplicationA");

        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        String topicName = System.getProperty("topic");

        Topic topic = session.createTopic(topicName);
        MessageConsumer consumer = session.createConsumer(topic);

        LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS
                + "ms timeout");

        // Synchronous message consumer
        int i = 1;
        while (true) {
            Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS);
            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    LOG.info("Got " + (i++) + ". message: " + text);
                }
            } else {
                break;
            }
        }

        consumer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(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);
    connection.start();/*from w w  w . j a v a 2  s.co  m*/
    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: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);
    connection.start();/*from ww  w.  j  av  a2  s. c  om*/
    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:ConsumerTool.java

public void run() {
    try {/*from  w  w w .j a va  2  s .c om*/
        running = true;

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        Connection connection = connectionFactory.createConnection();
        if (durable && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
            connection.setClientID(clientId);
        }
        connection.setExceptionListener(this);
        connection.start();

        session = connection.createSession(transacted, ackMode);
        if (topic) {
            destination = session.createTopic(subject);
        } else {
            destination = session.createQueue(subject);
        }

        replyProducer = session.createProducer(null);
        replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        MessageConsumer consumer = null;
        if (durable && topic) {
            consumer = session.createDurableSubscriber((Topic) destination, consumerName);
        } else {
            consumer = session.createConsumer(destination);
        }

        if (maxiumMessages > 0) {
            consumeMessagesAndClose(connection, session, consumer);
        } else {
            if (receiveTimeOut == 0) {
                consumer.setMessageListener(this);
            } else {
                consumeMessagesAndClose(connection, session, consumer, receiveTimeOut);
            }
        }

    } catch (Exception e) {
        System.out.println("[" + this.getName() + "] Caught: " + e);
        e.printStackTrace();
    }
}

From source file:com.fusesource.forge.jmstest.executor.BenchmarkConsumer.java

@Override
public Connection getConnection() throws Exception {
    Connection connection = super.getConnection();
    assert connection != null;

    // should have this usually, but definitely for durable subscriber
    if (connection.getClientID() == null || connection.getClientID().isEmpty()) {
        log().info("Setting client ID!! yip yip");
        connection.setClientID(getClientId());
    }//from w w w. ja  va  2  s  .  c om
    return connection;
}

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/** Run of Thread. */
public void run() {
    try {/*from ww  w. j a va  2 s.co m*/
        running = true;
        this.url = "failover://vm://" + this.brokerName;
        BSPActiveMQConnFactory connectionFactory = new BSPActiveMQConnFactoryImpl();
        connectionFactory.activeMQConnFactoryMethod(url);
        connectionFactory.setOptimizeAcknowledge(true);
        Connection connection = connectionFactory.createConnection();
        if (durable && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
            connection.setClientID(clientId);
        }
        connection.setExceptionListener(this);
        connection.start();
        session = connection.createSession(transacted, ackMode);
        if (topic) {
            destination = session.createTopic(subject);
        } else {
            destination = session.createQueue(subject);
        }
        replyProducer = session.createProducer(null);
        replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        if (durable && topic) {
            consumer = session.createDurableSubscriber((Topic) destination, consumerName);
        } else {
            consumer = session.createConsumer(destination);
        }
        consumeMessages(connection, session, consumer, receiveTimeOut);
        LOG.info("[ConsumerTool] has received " + this.messagesReceived + " object messages from <"
                + this.subject + ">.");
        LOG.info("[ConsumerTool] has received " + this.messageCount + " BSP messages from <" + this.subject
                + ">.");
        this.receiver.addMessageCount(this.messageCount);
        this.receiver.addMessageBytesCount(messageBytesCount);
    } catch (Exception e) {
        throw new RuntimeException("[ConsumerTool] caught: ", e);
    }
}

From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

@Override
public boolean connect() {
    connection = null;/* w w w .  java 2 s.co  m*/
    ActiveMQConnectionFactory connectionFactory = provider.getConnectionFactory();

    String ip = null;
    try {
        ip = Inet4Address.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        log.severe("Unable to get localhost IP address.");
    }
    Connection connectiontmp = null;
    try {
        connectiontmp = connectionFactory.createConnection();
        String url = "";
        if (Jenkins.getInstance() != null) {
            url = Jenkins.getInstance().getRootUrl();
        }
        connectiontmp.setClientID(provider.getName() + "_" + url + "_" + ip + "_" + jobname);
        connectiontmp.start();
    } catch (JMSException e) {
        log.severe("Unable to connect to " + provider.getBroker() + " " + e.getMessage());
        return false;
    }
    log.info("Connection started");
    connection = connectiontmp;
    return true;
}