Search in sources :

Example 16 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class JMSClient method sendToHornetQ.

private static void sendToHornetQ(String payload, String queueName) throws Exception {
    HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(HQ_USER).setPassword(HQ_PASSWD);
    hqMixIn.initialize();
    try {
        Session session = hqMixIn.getJMSSession();
        MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(queueName));
        producer.send(hqMixIn.createJMSMessage(payload));
        session.close();
        verifyOutputQueue(hqMixIn.createJMSSession());
    } finally {
        hqMixIn.uninitialize();
    }
}
Also used : MessageProducer(javax.jms.MessageProducer) HornetQMixIn(org.switchyard.component.test.mixins.hornetq.HornetQMixIn) Session(javax.jms.Session)

Example 17 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class JmsBindingTest method testRollbackB.

@Test
public void testRollbackB() throws Exception {
    String command = "rollback.B";
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_queueIn);
        TextMessage message = session.createTextMessage();
        message.setText(command);
        producer.send(message);
        session.close();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_queueOutA);
        TextMessage msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutB);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        consumer.close();
        consumer = session.createConsumer(_queueOutC);
        msg = TextMessage.class.cast(consumer.receive(1000));
        Assert.assertEquals(command, msg.getText());
        Assert.assertNull(consumer.receive(1000));
        session.close();
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 18 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class JmsBindingTest method testNonTransacted.

@Test
public void testNonTransacted() throws Exception {
    String command = "rollback.A";
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_queueInNoTx);
        TextMessage message = session.createTextMessage();
        message.setText(command);
        producer.send(message);
        session.close();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_queueOutA);
        Assert.assertNull(consumer.receive(1000));
        consumer = session.createConsumer(_queueOutB);
        Assert.assertNull(consumer.receive(1000));
        consumer = session.createConsumer(_queueOutC);
        Assert.assertNull(consumer.receive(1000));
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 19 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class ActiveMQClient method main.

/**
     * Only execution point for this application.
     * @param ignored not used.
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
    if (args.length != 0) {
        orders = args;
    }
    ActiveMQMixIn mixIn = new ActiveMQMixIn();
    try {
        Session session = mixIn.getSession();
        MessageProducer producer = session.createProducer(session.createQueue(ORDER_QUEUE));
        for (String order : orders) {
            final TextMessage message = session.createTextMessage();
            message.setText(order);
            producer.send(message);
        }
        session.close();
        session = mixIn.getSession();
        System.out.println("* * *  SHIPPING ORDERS  * * *");
        MessageConsumer consumer = session.createConsumer(session.createQueue(SHIPPING_QUEUE));
        Message msg = null;
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        System.out.println();
        System.out.println("* * *  PENDING ORDERS (FILLING STOCK)  * * *");
        consumer = session.createConsumer(session.createQueue(FILLING_STOCK_QUEUE));
        while ((msg = consumer.receive(1000)) != null) {
            if (msg instanceof TextMessage) {
                System.out.println(" - " + ((TextMessage) msg).getText());
            }
        }
        session.close();
        Thread.sleep(2000);
    } finally {
        mixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) ActiveMQMixIn(org.switchyard.component.test.mixins.activemq.ActiveMQMixIn) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 20 with Session

use of javax.jms.Session in project spring-framework by spring-projects.

the class AbstractMessageListenerContainer method doInvokeListener.

/**
	 * Invoke the specified listener as Spring SessionAwareMessageListener,
	 * exposing a new JMS Session (potentially with its own transaction)
	 * to the listener if demanded.
	 * @param listener the Spring SessionAwareMessageListener to invoke
	 * @param session the JMS Session to operate on
	 * @param message the received JMS Message
	 * @throws JMSException if thrown by JMS API methods
	 * @see SessionAwareMessageListener
	 * @see #setExposeListenerSession
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void doInvokeListener(SessionAwareMessageListener listener, Session session, Message message) throws JMSException {
    Connection conToClose = null;
    Session sessionToClose = null;
    try {
        Session sessionToUse = session;
        if (!isExposeListenerSession()) {
            // We need to expose a separate Session.
            conToClose = createConnection();
            sessionToClose = createSession(conToClose);
            sessionToUse = sessionToClose;
        }
        // Actually invoke the message listener...
        listener.onMessage(message, sessionToUse);
        // Clean up specially exposed Session, if any.
        if (sessionToUse != session) {
            if (sessionToUse.getTransacted() && isSessionLocallyTransacted(sessionToUse)) {
                // Transacted session created by this container -> commit.
                JmsUtils.commitIfNecessary(sessionToUse);
            }
        }
    } finally {
        JmsUtils.closeSession(sessionToClose);
        JmsUtils.closeConnection(conToClose);
    }
}
Also used : Connection(javax.jms.Connection) Session(javax.jms.Session)

Aggregations

Session (javax.jms.Session)231 MessageProducer (javax.jms.MessageProducer)106 Test (org.junit.Test)98 Connection (javax.jms.Connection)88 Message (javax.jms.Message)73 TextMessage (javax.jms.TextMessage)69 JMSException (javax.jms.JMSException)58 MessageConsumer (javax.jms.MessageConsumer)53 Topic (javax.jms.Topic)36 ConnectionFactory (javax.jms.ConnectionFactory)31 Destination (javax.jms.Destination)27 Queue (javax.jms.Queue)26 ObjectMessage (javax.jms.ObjectMessage)24 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MapMessage (javax.jms.MapMessage)12 MessageCreator (org.springframework.jms.core.MessageCreator)12 BytesMessage (javax.jms.BytesMessage)11 TopicSession (javax.jms.TopicSession)9