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.firstopen.singularity.util.JMSUtil.java

public static void deliverMessageToQueue(String host, String queueName, String xml) {
    log.debug("IntegrationMod.deliverMessageToQueue queueName = " + queueName + " and doc = " + xml);
    MessageProducer m_sender = null;/*  w w  w  .ja v  a2  s.c o m*/
    Session m_session = null;
    Connection connection = null;

    char test = queueName.charAt(0);

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

    try {

        InitialContext context = JNDIUtil.getInitialContext(host);

        ConnectionFactory qcf = (ConnectionFactory) context.lookup("ConnectionFactory");

        Queue queue = (Queue) context.lookup("queue/" + queueName);

        connection = qcf.createConnection();

        m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        m_sender = m_session.createProducer(queue);

        TextMessage message = m_session.createTextMessage();

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

        message.setText(xml);

        m_sender.send(message);

    } catch (Exception e) {
        log.error("IntegrationMod.deliverMessageToQueue() Exception = ", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.error("unable ot close JMS Connection", e);
            }
        }
    }
}

From source file:org.apache.axis2.transport.jms.JMSUtils.java

/**
 * This is a JMS spec independent method to create a Session. Please be cautious when
 * making any changes//w w  w.j a v  a 2 s  . co m
 *
 * @param connection the JMS Connection
 * @param transacted should the session be transacted?
 * @param ackMode the ACK mode for the session
 * @param jmsSpec11 should we use the JMS 1.1 API?
 * @param isQueue is this Session to deal with a Queue?
 * @return a Session created for the given information
 * @throws JMSException on errors, to be handled and logged by the caller
 */
public static Session createSession(Connection connection, boolean transacted, int ackMode, boolean jmsSpec11,
        Boolean isQueue) throws JMSException {

    if (jmsSpec11 || 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:Log4jJMSAppenderExample.java

public Log4jJMSAppenderExample() throws Exception {
    // create a logTopic topic consumer
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection conn = factory.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    conn.start();//www  .j a va 2s  . co m
    MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic"));
    consumer.setMessageListener(this);
    // log a message
    Logger log = Logger.getLogger(Log4jJMSAppenderExample.class);
    log.info("Test log");
    // clean up
    Thread.sleep(1000);
    consumer.close();
    sess.close();
    conn.close();
    System.exit(1);
}

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

@Bean
Session session(Connection amqConnection) throws JMSException {
    return amqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:com.tremolosecurity.provisioning.util.PooledMessageProducerFactory.java

@Override
public PooledObject<MessageProducerHolder> makeObject() throws Exception {
    Connection con = prov.getQueueConnection();
    Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue q = session.createQueue(taskQueueName);
    MessageProducer mp = session.createProducer(q);
    return new DefaultPooledObject(new MessageProducerHolder(con, mp, session));
}

From source file:org.apache.activemq.JmsTopicSendReceiveWithTwoConnectionsTest.java

protected Session createSendSession(Connection sendConnection) throws Exception {
    return sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:sk.seges.test.jms.multiple.QueueSendReceiveTest.java

@Test
public void testSend() throws Exception {
    Connection connection = testConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(testQueueA);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    TextMessage message = session.createTextMessage("test text");
    producer.send(message);//from  w  ww. ja  v a 2s . com
    connection.close();
}

From source file:org.apache.activemq.JmsTopicSendReceiveWithTwoConnectionsTest.java

protected Session createReceiveSession(Connection receiveConnection) throws Exception {
    return receiveConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueSendReceiveTest.java

@Test
public void testSend() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(activeMQTestQueueA);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    TextMessage message = session.createTextMessage("test text");
    producer.send(message);/*from  w  w  w . ja  v a  2  s  .co m*/
    connection.close();
}

From source file:org.sample.TimeController.java

@RequestMapping(method = RequestMethod.GET, value = { "/tick" })
public ModelAndView get() throws Exception {
    Connection connection = jmsFactory.createConnection();
    Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    SecureRandom srandom = new SecureRandom();
    String rand = new BigInteger(176, srandom).toString(32);
    String q = Long.toString(System.currentTimeMillis()) + "-" + rand;
    Destination destination = jmsSession.createQueue(q);

    Timer timer = new Timer();
    long delay = 1 * 1000;
    timer.schedule(new TickTask(jmsSession, destination), 0, delay);

    ModelAndView mav = new ModelAndView("client");
    mav.addObject("queue", destination.toString());
    return mav;//from   w ww  .j a v a  2 s.  com
}