Example usage for javax.jms ConnectionFactory createConnection

List of usage examples for javax.jms ConnectionFactory createConnection

Introduction

In this page you can find the example usage for javax.jms ConnectionFactory createConnection.

Prototype


Connection createConnection() throws JMSException;

Source Link

Document

Creates a connection with the default user identity.

Usage

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

@Bean
Connection amqConnection(ConnectionFactory amqConnectionFactory) throws JMSException {
    Connection connection = amqConnectionFactory.createConnection();
    connection.start();//  ww  w  . j  a v a 2s.  c  om
    connection.setExceptionListener(e -> System.out.println("JMS Exception occurred. Shutting down client."));
    return connection;
}

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();//from w  w  w . j a  va2s.c  o  m

    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);

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

From source file:org.apache.storm.jms.spout.JmsSpoutTest.java

public Message sendMessage(ConnectionFactory connectionFactory, Destination destination) throws JMSException {
    Session mySess = connectionFactory.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
    MessageProducer producer = mySess.createProducer(destination);
    TextMessage msg = mySess.createTextMessage();
    msg.setText("Hello World");
    log.debug("Sending Message: " + msg.getText());
    producer.send(msg);//from  www  . j  a  va2s.co m
    return msg;
}

From source file:io.datalayer.activemq.consumer.SpringConsumer.java

public void start() throws JMSException {
    String selector = "next = '" + myId + "'";

    try {/*  w ww  .j av  a 2  s  .com*/
        ConnectionFactory factory = template.getConnectionFactory();
        connection = factory.createConnection();

        // we might be a reusable connection in spring
        // so lets only set the client ID once if its not set
        synchronized (connection) {
            if (connection.getClientID() == null) {
                connection.setClientID(myId);
            }
        }

        connection.start();

        session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
        consumer = session.createConsumer(destination, selector, false);
        consumer.setMessageListener(this);
    } catch (JMSException ex) {
        LOG.error("", ex);
        throw ex;
    }
}

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

/**
 * Obtain a JMS Session that is synchronized with the current transaction, if any.
 * @param cf the ConnectionFactory to obtain a Session for
 * @param existingCon the existing JMS Connection to obtain a Session for
 * (may be {@code null})//from  ww  w  . ja v a 2 s .  c  o m
 * @param synchedLocalTransactionAllowed whether to allow for a local JMS transaction
 * that is synchronized with a Spring-managed transaction (where the main transaction
 * might be a JDBC-based one for a specific DataSource, for example), with the JMS
 * transaction committing right after the main transaction. If not allowed, the given
 * ConnectionFactory needs to handle transaction enlistment underneath the covers.
 * @return the transactional Session, or {@code null} if none found
 * @throws JMSException in case of JMS failure
 */
@Nullable
public static Session getTransactionalSession(final ConnectionFactory cf,
        @Nullable final Connection existingCon, final boolean synchedLocalTransactionAllowed)
        throws JMSException {

    return doGetTransactionalSession(cf, new ResourceFactory() {
        @Override
        @Nullable
        public Session getSession(JmsResourceHolder holder) {
            return holder.getSession(Session.class, existingCon);
        }

        @Override
        @Nullable
        public Connection getConnection(JmsResourceHolder holder) {
            return (existingCon != null ? existingCon : holder.getConnection());
        }

        @Override
        public Connection createConnection() throws JMSException {
            return cf.createConnection();
        }

        @Override
        public Session createSession(Connection con) throws JMSException {
            return con.createSession(synchedLocalTransactionAllowed, Session.AUTO_ACKNOWLEDGE);
        }

        @Override
        public boolean isSynchedLocalTransactionAllowed() {
            return synchedLocalTransactionAllowed;
        }
    }, true);
}

From source file:org.fusesource.stompjms.JmsTestSupport.java

protected void sendMessages(Destination destination, int count) throws Exception {
    ConnectionFactory factory = createConnectionFactory();
    Connection connection = factory.createConnection();
    connection.start();//from   w  ww  .j av  a2s  .c  om
    sendMessages(connection, destination, count);
    connection.close();
}

From source file:io.fabric8.mq.controller.coordination.BrokerJMXPropertiesTest.java

@Test
public void testNumberOfTopicProducers() throws Exception {
    String uriString = brokerService.getDefaultSocketURIString();
    ConnectionFactory factory = new ActiveMQConnectionFactory(uriString);
    Connection connection = factory.createConnection();
    connection.start();/*from w  w w . ja va  2s  .  c  o  m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    int numberOfProducers = 10;
    List<MessageProducer> producers = new ArrayList<>();
    Topic topic = session.createTopic("topic.test");
    for (int i = 0; i < numberOfProducers; i++) {

        MessageProducer producer = session.createProducer(topic);
        producers.add(producer);

    }
    ObjectName root = BrokerJmxUtils.getRoot(client);
    Assert.assertNotNull(root);
    List<ObjectName> list = BrokerJmxUtils.getDestinations(client, root, "Topic");
    Assert.assertNotNull(list);
    Assert.assertFalse(list.isEmpty());
    ObjectName result = null;
    for (ObjectName objectName : list) {
        if (objectName.getKeyProperty("destinationName").equals("topic.test")) {
            result = objectName;
        }
    }
    Assert.assertNotNull(result);
    Object producerCount = BrokerJmxUtils.getAttribute(client, result, "ProducerCount");
    Assert.assertNotNull(producerCount);
    int pc = Integer.parseInt(producerCount.toString());
    Assert.assertEquals(pc, numberOfProducers);
    connection.close();
}

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

private void consumer() throws JMSException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    Connection connection = connectionFactory.createConnection();
    connection.start();//from w  w w  .  j  a va  2 s  .c  o m

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

    latch.countDown();

    for (int index = 0; index < outputFeedNames.length; ++index) {
        MapMessage m = (MapMessage) consumer.receive();
        System.out.println("Consumed: " + m.toString());
        assertMessage(m);
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_NAMES.getName()),
                outputFeedNames[index]);
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_PATHS.getName()),
                outputFeedPaths[index]);
    }
    connection.close();
}

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

private void createAndStartConnection(String implementation, String userName, String password, String url)
        throws JMSException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException {

    Class<ConnectionFactory> clazz = (Class<ConnectionFactory>) MessageProducer.class.getClassLoader()
            .loadClass(implementation);/*ww  w.  j  a  v  a2  s .c o m*/

    ConnectionFactory connectionFactory = clazz.getConstructor(String.class, String.class, String.class)
            .newInstance(userName, password, url);

    connection = connectionFactory.createConnection();
    connection.start();
}

From source file:org.apache.ivory.messaging.MessageProducer.java

private void createAndStartConnection(String implementation, String userName, String password, String url)
        throws JMSException, ClassNotFoundException, IllegalArgumentException, SecurityException,
        InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    Class<ConnectionFactory> clazz = (Class<ConnectionFactory>) MessageProducer.class.getClassLoader()
            .loadClass(implementation);/*from  w w  w.j a  va 2  s .c o m*/

    ConnectionFactory connectionFactory = clazz.getConstructor(String.class, String.class, String.class)
            .newInstance(userName, password, url);

    connection = connectionFactory.createConnection();
    connection.start();
}