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: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();/*from w  w w.j  a  va  2s.co m*/
    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;
            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.hawkular.apm.server.jms.AbstractPublisherJMS.java

@PostConstruct
public void init() {
    try {//w ww  .ja  va 2 s  . c om
        InitialContext context = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:/APMJMSCF");
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // TODO: Transacted?
        Destination destination = (Destination) context.lookup(getDestinationURI());
        producer = session.createProducer(destination);
        connection.start();
    } catch (Exception e) {
        msgLog.errorFailedToInitPublisher(getDestinationURI(), e);
    }
}

From source file:org.eclipse.smila.connectivity.queue.AbstractBrokerPropertiesConfigImpl.java

/**
 * {@inheritDoc}//from   w  w w .  j ava  2  s.c  o  m
 * 
 * @see org.eclipse.smila.connectivity.queue.ConnectivityBroker#start()
 */
public synchronized void start() throws BrokerException {
    Properties properties;
    try {
        properties = loadJMSProperties();
    } catch (final Exception e) {
        throw new BrokerException("Unable to load JMS properties", e);
    }
    Context ctx;
    try {
        ctx = new InitialContext(properties);
        System.setProperty(PROPERTY_DEFAULT_DIRECTORY_PREFIX,
                properties.getProperty(PROPERTY_DEFAULT_DIRECTORY_PREFIX));
        final ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
        _connection = factory.createConnection();
        _connection.start();
        _session = _connection.createSession(true, Session.SESSION_TRANSACTED);
        _session.createQueue(DLQ);
    } catch (final Exception e) {
        throw new BrokerException("Error initializing Queue Broker with JMS properties resp. configuration", e);
    } finally {
        if (_session != null) {
            try {
                _session.commit();
            } catch (final JMSException e) {
                ;// nothing
            }
            try {
                _session.close();
            } catch (final JMSException e) {
                ;// nothing
            }
        }
    }
}

From source file:org.jbpm.bpel.tutorial.invoice.ComputePricePT_Impl.java

protected void initJmsObjects() throws NamingException, JMSException {
    Context initialContext = new InitialContext();
    try {/*from w  w  w  .  j a  va 2  s. com*/
        Context environmentContext = (Context) initialContext.lookup("java:comp/env");
        invoiceDestination = (Destination) environmentContext.lookup(INVOICE_DESTINATION_NAME);
        log.debug("Retrieved destination: " + INVOICE_DESTINATION_NAME);

        ConnectionFactory jmsConnectionFactory = (ConnectionFactory) environmentContext
                .lookup(CONNECTION_FACTORY_NAME);
        jmsConnection = jmsConnectionFactory.createConnection();
        log.debug("Created JMS connection: factory=" + CONNECTION_FACTORY_NAME);
    } finally {
        initialContext.close();
    }
}

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

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

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

    latch.countDown();
    verifyMesssage(consumer);

    connection.close();
}

From source file:org.jbpm.bpel.tutorial.shipping.ShippingPT_Impl.java

protected void initJmsObjects() throws NamingException, JMSException {
    Context initialContext = new InitialContext();
    try {//from   ww  w  . j a v  a2s  . c o  m
        Context environmentContext = (Context) initialContext.lookup("java:comp/env");
        shippingDestination = (Destination) environmentContext.lookup(SHIPPING_DESTINATION_NAME);
        log.debug("Retrieved destination: " + SHIPPING_DESTINATION_NAME);

        ConnectionFactory jmsConnectionFactory = (ConnectionFactory) environmentContext
                .lookup(CONNECTION_FACTORY_NAME);
        jmsConnection = jmsConnectionFactory.createConnection();
        log.debug("Created JMS connection: factory=" + CONNECTION_FACTORY_NAME);
    } finally {
        initialContext.close();
    }
}

From source file:org.opendds.esb.actions.routing.JmsRouter.java

public JmsRouter(ConfigTree config) throws ConfigurationException {
    super(config);
    try {//from   w  w w .j  a  v  a 2 s. com
        InitialContext ctx = new InitialContext();

        ConnectionFactory cf = (ConnectionFactory) ctx
                .lookup(ConfigTreeHelper.requireAttribute(config, "connection-factory"));

        connection = cf.createConnection();

        destination = (Destination) ctx.lookup(ConfigTreeHelper.requireAttribute(config, "dest-name"));

        messages = new ThreadedQueue<Message>(new MessageListener(),
                ConfigTreeHelper.getAttribute(config, "maxThreads"));

    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}

From source file:com.nesscomputing.jms.activemq.ServiceDiscoveryTransportFactoryTest.java

@Test
public void testDiscoveryUri() throws Exception {
    final ServiceInformation vmbrokerInfo = new ServiceInformation("vmbroker", null, UUID.randomUUID(),
            ImmutableMap.of("uri", "vm://disco-test-broker-" + uniqueId));

    final Config config = Config
            .getFixedConfig(ImmutableMap.of("ness.jms.connection-url", "srvc://vmbroker?discoveryId=%s"));

    Guice.createInjector(new AbstractModule() {
        @Override/*from  w w w .  j  a  v  a  2 s  . c  o m*/
        protected void configure() {
            binder().requireExplicitBindings();
            binder().disableCircularProxies();

            install(new ConfigModule(config));
            install(new JmsModule(config, "test"));
            install(new DiscoveryJmsModule(config));

            bind(ReadOnlyDiscoveryClient.class).toInstance(
                    MockedReadOnlyDiscoveryClient.builder().addServiceInformation(vmbrokerInfo).build());
        }
    }).injectMembers(this);

    final ConnectionFactory directFactory = new ActiveMQConnectionFactory(
            "vm://disco-test-broker-" + uniqueId + "?broker.persistent=false");

    final Connection directConnection = directFactory.createConnection();
    directConnection.start();
    try {
        sendTestMessage(directConnection);
        consumeTestMessage();
    } finally {
        directConnection.stop();
        directConnection.close();
    }
}

From source file:org.apache.juddi.subscription.notify.AMQPNotifier.java

@Override
public DispositionReport notifySubscriptionListener(NotifySubscriptionListener body)
        throws DispositionReportFaultMessage, RemoteException {
    Connection connection = null;
    Context context = null;/* w  w w  .  j  av  a  2  s  .  c  om*/
    boolean success = false;
    String err = null;
    try {
        if (destination != null && exchangeType != null && exchangeName != null) {
            log.info("Sending notification AMQP to " + destination);
            Properties properties = new Properties();

            properties.put("java.naming.factory.initial",
                    "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
            properties.put("connectionfactory.qpidConnectionfactory", destination);
            properties.put("destination." + exchangeName, exchangeType);

            context = new InitialContext(properties);

            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
            connection = connectionFactory.createConnection();
            connection.start();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destinationLocal = (Destination) context.lookup(exchangeName);

            MessageProducer messageProducer = session.createProducer(destinationLocal);

            String subscriptionResultXML = JAXBMarshaller.marshallToString(body,
                    JAXBMarshaller.PACKAGE_SUBSCR_RES);
            TextMessage message = session.createTextMessage(subscriptionResultXML);
            messageProducer.send(message);
            success = true;

        }
    } catch (Exception e) {
        e.printStackTrace();
        log.error("Error deliverying AMQP subscription " + e.getMessage());
        log.debug("Error deliverying AMQP subscription " + e.getMessage(), e);
        err = e.getMessage();

    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (JMSException ex) {
            log.error(null, ex);
        }
        try {
            if (context != null) {
                context.close();
            }
        } catch (NamingException ex) {
            log.error(null, ex);
        }
    }
    if (!success) {
        throw new DispositionReportFaultMessage(err, null);
    }
    DispositionReport dr = new DispositionReport();
    Result res = new Result();
    dr.getResult().add(res);

    return dr;
}

From source file:org.apache.flume.source.jms.TestIntegrationActiveMQ.java

private void putQueue(List<String> events) throws Exception {
    ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_BIND_URL);
    Connection connection = factory.createConnection();
    connection.start();//from  w ww .  j ava2  s.  co m

    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(DESTINATION_NAME);
    MessageProducer producer = session.createProducer(destination);

    for (String event : events) {
        TextMessage message = session.createTextMessage();
        message.setText(event);
        producer.send(message);
    }
    session.commit();
    session.close();
    connection.close();
}