Example usage for javax.jms QueueConnectionFactory createQueueConnection

List of usage examples for javax.jms QueueConnectionFactory createQueueConnection

Introduction

In this page you can find the example usage for javax.jms QueueConnectionFactory createQueueConnection.

Prototype


QueueConnection createQueueConnection() throws JMSException;

Source Link

Document

Creates a queue connection with the default user identity.

Usage

From source file:org.sample.jms.SampleQueueSender.java

public void sendMessages() throws NamingException, JMSException {

    PropertyConfigurator.configure("log4j.properties");
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
    properties.put(QUEUE_NAME_PREFIX + queueName, queueName);
    InitialContext ctx = new InitialContext(properties);
    // Lookup connection factory
    QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
    queueConnection = connFactory.createQueueConnection();
    queueConnection.start();/*  www. j  a v a2  s  .c om*/
    queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    Queue queue = (Queue) ctx.lookup(queueName);
    QueueSender queueSender = queueSession.createSender(queue);
    TextMessage textMessage;

    for (long i = 0; i < RECORD_COUNT; i++) {
        // create the message to send
        textMessage = queueSession.createTextMessage("Test Message Content" + messageCount);
        // Send message
        queueSender.send(textMessage);
        messageCount++;
    }
    queueSender.close();
    queueSession.close();
    queueConnection.close();
}

From source file:jms.queue.TestQueueSender.java

public void init(PublisherConfig conf) throws NamingException, JMSException {
    String queueName = conf.getQueueName();
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, conf.getInitialContextFactory());
    properties.put(conf.getConnectionFactoryPrefix() + "." + conf.getConnectionFactoryName(),
            conf.getTCPConnectionURL());
    properties.put("queue." + queueName, queueName);
    InitialContext ctx = new InitialContext(properties);
    // Lookup connection factory
    QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(conf.getConnectionFactoryName());
    queueConnection = connFactory.createQueueConnection();
    queueConnection.start();/*w  w  w.j a  v  a  2 s .  c o m*/
    if (conf.isTransactional()) {
        queueSession = queueConnection.createQueueSession(true, 0);
    } else {
        queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    }
    //        Queue queue = (Queue)ctx.lookup(queueName);
    Queue queue = queueSession.createQueue(queueName);
    queueSender = queueSession.createSender(queue);
    config = conf;

}

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageBrowser.java

public QueueMessageBrowser(String host, String port, String userName, String password, String destination,
        int printNumberOfMessagesPer, boolean isToPrintMessage) {
    this.hostName = host;
    this.port = port;
    this.queueName = destination;
    this.printNumberOfMessagesPer = printNumberOfMessagesPer;
    this.isToPrintEachMessage = isToPrintMessage;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
    properties.put("queue." + queueName, queueName);

    log.info("getTCPConnectionURL(userName,password) = " + getTCPConnectionURL(userName, password));

    try {/*from  w w  w  .jav a2  s.  c  om*/
        InitialContext ctx = new InitialContext(properties);
        // Lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        Queue queue = (Queue) ctx.lookup(queueName);
        queueBrowser = queueSession.createBrowser(queue);

    } catch (NamingException e) {
        log.error("Error while looking up for queue", e);
    } catch (JMSException e) {
        log.error("Error while initializing queue connection", e);
    }

}

From source file:org.apache.cactus.spi.server.MessageDrivenBeanRedirector.java

/**
 * Called by the container to create an instance of this Message Driven
 * bean./*from w w w  . j  av a  2s.c  om*/
 *
 * @exception CreateException see the EJB specification
 */
public void ejbCreate() throws CreateException {
    LOGGER.debug("------------- MDB redirector service created");

    try {
        InitialContext initContext = new InitialContext();
        QueueConnectionFactory qcf = (QueueConnectionFactory) initContext.lookup("java:comp/env/jms/QCF1");
        connection = qcf.createQueueConnection();
        session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        connection.start();
    } catch (Exception e) {
        throw new EJBException("Failed to initialize MyMDB", e);
    }
}

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

/**
 * Obtain a JMS QueueSession that is synchronized with the current transaction, if any.
 * <p>Mainly intended for use with the JMS 1.0.2 API.
 * @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 w ww. j  av a  2 s .  c om*/
 * @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 QueueSession getTransactionalQueueSession(final QueueConnectionFactory cf,
        @Nullable final QueueConnection existingCon, final boolean synchedLocalTransactionAllowed)
        throws JMSException {

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

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

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

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

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

From source file:org.wso2.carbon.oc.agent.publisher.mb.MBPublisher.java

/**
 * @param queueName   - String mb queue name
 * @param jsonMessage - String mb queue message json string
 */// w  w w .j  a  v a2 s .  c o m
public void sendMessages(String queueName, String jsonMessage) {

    try {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
        properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(username, password));
        properties.put(QUEUE_NAME_PREFIX + queueName, queueName);
        InitialContext ctx = new InitialContext(properties);
        // lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        QueueConnection queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        // send message
        Queue queue = (Queue) ctx.lookup(queueName);
        // create the message to send

        TextMessage textMessage = queueSession.createTextMessage(jsonMessage);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.send(textMessage);
        queueSender.close();
        queueSession.close();
        queueConnection.close();
    } catch (JMSException e) {
        logger.error("MBPublisher connection down", e);
    } catch (NamingException e) {
        logger.error("Naming error", e);
    }

}

From source file:org.wso2.carbon.andes.ui.client.QueueBrowserClient.java

/**
 * Return a lazy-loading iterator (java.util.Enumeration) of the messages of the given queue.
 * @return java.util.Enumeration//from  w  ww  . jav a2s .  c  o  m
 */
public Enumeration browseQueue() {

    Enumeration queueContentsEnu = null;
    try {
        InitialContext ctx = new InitialContext(properties);
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        Queue queue = (Queue) ctx.lookup(nameOfQueue);
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueBrowser = queueSession.createBrowser(queue);
        queueConnection.start();

        queueContentsEnu = queueBrowser.getEnumeration();

    } catch (NamingException e) {
        log.error("Error browsing queue.", e);
    } catch (JMSException e) {
        log.error("Error browsing queue.", e);
    }
    return queueContentsEnu;
}

From source file:org.dawnsci.commandserver.core.producer.Broadcaster.java

private void createConnection() throws Exception {

    QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
    this.connection = connectionFactory.createQueueConnection();
    this.qSes = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    this.queue = qSes.createQueue(queueName);

    this.session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Topic topic = session.createTopic(topicName);
    this.topicProducer = session.createProducer(topic);

    connection.start();/*from w  w  w .j a v  a 2  s.c o  m*/

}

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageReceiver.java

public QueueMessageReceiver(String connectionString, String hostName, String port, String userName,
        String password, String queueName, int ackMode, boolean useMessageListener,
        AtomicInteger messageCounter, int delayBetweenMessages, int printNumberOfMessagesPer,
        boolean isToPrintEachMessage, String fileToWriteReceivedMessages, int stopAfter, int ackAfterEach,
        int commitAfterEach, int rollbackAfterEach) {

    this.hostName = hostName;
    this.port = port;
    this.connectionString = connectionString;
    this.useMessageListener = useMessageListener;
    this.delayBetweenMessages = delayBetweenMessages;
    this.messageCounter = messageCounter;
    this.queueName = queueName;
    this.printNumberOfMessagesPer = printNumberOfMessagesPer;
    this.isToPrintEachMessage = isToPrintEachMessage;
    this.fileToWriteReceivedMessages = fileToWriteReceivedMessages;
    this.stopAfter = stopAfter;
    this.ackAfterEach = ackAfterEach;
    this.commitAfterEach = commitAfterEach;
    this.rollbackAfterEach = rollbackAfterEach;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
    properties.put("queue." + queueName, queueName);

    try {/*from   w ww  .  j av  a  2s  .  c  o m*/
        InitialContext ctx = new InitialContext(properties);
        // Lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        if (ackMode == QueueSession.SESSION_TRANSACTED) {
            queueSession = queueConnection.createQueueSession(true, ackMode);
        } else {
            queueSession = queueConnection.createQueueSession(false, ackMode);
        }
        Queue queue = (Queue) ctx.lookup(queueName);
        queueReceiver = queueSession.createReceiver(queue);

    } catch (NamingException e) {
        log.error("Error while looking up for queue", e);
    } catch (JMSException e) {
        log.error("Error while initializing queue connection", e);
    }

}

From source file:org.easybatch.jms.JmsIntegrationTest.java

@Test
public void testJmsRecordWriter() throws Exception {
    Context jndiContext = getJndiContext();
    Queue queue = (Queue) jndiContext.lookup("q");
    QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext
            .lookup("QueueConnectionFactory");
    QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queueConnection.start();// w w  w  .java2 s.  co  m

    String dataSource = "foo" + LINE_SEPARATOR + "bar";

    aNewJob().reader(new StringRecordReader(dataSource)).processor(new JmsMessageTransformer(queueSession))
            .writer(new JmsQueueRecordWriter(queueConnectionFactory, queue)).call();

    // Assert that queue contains 2 messages: "foo" and "bar"
    QueueBrowser queueBrowser = queueSession.createBrowser(queue);
    Enumeration enumeration = queueBrowser.getEnumeration();

    assertThat(enumeration.hasMoreElements()).isTrue();
    TextMessage message1 = (TextMessage) enumeration.nextElement();
    assertThat(message1.getText()).isEqualTo("foo");

    assertThat(enumeration.hasMoreElements()).isTrue();
    TextMessage message2 = (TextMessage) enumeration.nextElement();
    assertThat(message2.getText()).isEqualTo("bar");

    assertThat(enumeration.hasMoreElements()).isFalse();

    queueSession.close();
    queueConnection.close();
}