Example usage for javax.jms Connection createSession

List of usage examples for javax.jms Connection createSession

Introduction

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

Prototype


Session createSession(boolean transacted, int acknowledgeMode) throws JMSException;

Source Link

Document

Creates a Session object, specifying transacted and acknowledgeMode .

Usage

From source file:org.wso2.carbon.bpmn.extensions.jms.JMSUtils.java

/**
 *
 * @param connection//from  ww  w. j  a v a2s .  co m
 * @param transacted
 * @param ackMode
 * @param isQueue
 * @return
 * @throws JMSException
 */
public static Session createSession(Connection connection, boolean transacted, int ackMode, Boolean isQueue)
        throws JMSException {
    if (isQueue == null) {
        return connection.createSession(transacted, ackMode);
    } else {
        if (isQueue) {
            return ((QueueConnection) connection).createQueueSession(transacted, ackMode);
        } else {
            return ((TopicConnection) connection).createTopicSession(transacted, ackMode);
        }
    }
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static Connection registerListenerOnQueue(MessageListener listener, String queueName) throws Exception {
    InitialContext jndiContext = JNDIUtil.getInitialContext();
    Queue queue = (Queue) jndiContext.lookup("queue/" + queueName);
    ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
    Connection connection = qcf.createConnection();
    Session m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer m_receiver = m_session.createConsumer(queue);
    m_receiver.setMessageListener(listener);
    return connection;
}

From source file:nl.nn.adapterframework.extensions.tibco.TibcoUtils.java

public static long getQueueMessageCount(String provUrl, String authAlias, String userName, String password,
        String queueName, String messageSelector) throws JMSException {
    Connection connection = null;
    Session jSession = null;/*ww w.ja  v  a2s . com*/
    try {
        connection = getConnection(provUrl, authAlias, userName, password);
        jSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        return getQueueMessageCount(jSession, queueName, messageSelector);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.warn("Exception on closing connection", e);
            }
        }
    }
}

From source file:nl.nn.adapterframework.extensions.tibco.TibcoUtils.java

/**
 * return -1: no message found//from  w  w w. j  a va2  s  . c o  m
 * return -2: message found, but not of type Message.
 */
public static long getQueueFirstMessageAge(String provUrl, String authAlias, String userName, String password,
        String queueName, String messageSelector) throws JMSException {
    Connection connection = null;
    Session jSession = null;
    try {
        connection = getConnection(provUrl, authAlias, userName, password);
        jSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        return getQueueFirstMessageAge(jSession, queueName, messageSelector);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.warn("Exception on closing connection", e);
            }
        }
    }
}

From source file:com.mirth.connect.connectors.jms.JmsDispatcherTests.java

@BeforeClass
public static void beforeClass() throws Exception {
    JmsDispatcherProperties properties = getInitialProperties();

    if (properties.isUseJndi()) {
        connectionFactory = lookupConnectionFactoryWithJndi(properties);
    } else {//from w  w w .j  a va 2 s .com
        String className = properties.getConnectionFactoryClass();
        connectionFactory = (ConnectionFactory) Class.forName(className).newInstance();
    }

    BeanUtils.populate(connectionFactory, properties.getConnectionProperties());

    Connection connection = connectionFactory.createConnection(properties.getUsername(),
            properties.getPassword());
    connection.start();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:org.dawnsci.commandserver.core.util.JSONUtils.java

/**
 * Generic way of sending a topic notification
 * @param connection - does not get closed afterwards nust be started before.
 * @param message/*from  w  ww . j a  va  2 s. c o m*/
 * @param topicName
 * @param uri
 * @throws Exception
 */
private static final void sendTopic(Connection connection, Object message, String topicName, URI uri)
        throws Exception {

    // JMS messages are sent and received using a Session. We will
    // create here a non-transactional session object. If you want
    // to use transactions you should set the first parameter to 'true'
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    try {
        Topic topic = session.createTopic(topicName);

        MessageProducer producer = session.createProducer(topic);

        final ObjectMapper mapper = new ObjectMapper();

        // Here we are sending the message out to the topic
        TextMessage temp = session.createTextMessage(mapper.writeValueAsString(message));
        producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000);

    } finally {
        session.close();
    }
}

From source file:com.mirth.connect.connectors.jms.JmsReceiverTests.java

@BeforeClass
public static void beforeClass() throws Exception {
    JmsReceiverProperties properties = getInitialProperties();

    if (properties.isUseJndi()) {
        connectionFactory = lookupConnectionFactoryWithJndi(properties);
    } else {/*  ww w .  j av  a 2s  .c o  m*/
        String className = properties.getConnectionFactoryClass();
        connectionFactory = (ConnectionFactory) Class.forName(className).newInstance();
    }

    BeanUtils.populate(connectionFactory, properties.getConnectionProperties());

    Connection connection = connectionFactory.createConnection(properties.getUsername(),
            properties.getPassword());
    connection.start();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

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})/*w  ww . java 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.firstopen.singularity.util.JMSUtil.java

public static void deliverMessageToTopic(String host, String topicName, String xml) {
    log.debug("IntegrationMod.deliverMessageToQueue queueName = " + topicName + " and doc = " + xml);

    char test = topicName.charAt(0);

    if (test == '/')
        topicName = topicName.substring(1);

    try {//from  w w w .  j a v a  2s .  c om

        InitialContext context = JNDIUtil.getInitialContext(host);

        Connection connection = null;
        Session session = null;
        MessageProducer publisher = null;
        ConnectionFactory tcf = (ConnectionFactory) context.lookup("ConnectionFactory");

        Topic topic = (Topic) context.lookup(topicName);

        connection = tcf.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        publisher = session.createProducer(topic);

        TextMessage message = session.createTextMessage();

        log.debug("message value is -> " + xml);

        message.setText(xml);

        publisher.send(message);

    } catch (Exception e) {
        log.error("unable to send message on topic", e);
    } finally {

    }
}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

protected static void produce(Connection connection, Queue queue, int messageCount, int messageSize)
        throws JMSException, IOException, XAException {
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer producer = session.createProducer(queue);

    for (int i = 0; i < messageCount; i++) {
        TextMessage helloMessage = session.createTextMessage(StringUtils.repeat("a", messageSize));
        producer.send(helloMessage);/*w w  w. jav  a 2 s .  c  o m*/
        session.commit();

    }
}