Search in sources :

Example 11 with QueueSession

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException.

@Test
public void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException() throws Exception {
    Queue destination = mock(Queue.class);
    final TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
    // Reply-To is queried when response is being created...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseTextMessage = mock(TextMessage.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    willThrow(new JMSException("Doe!")).given(messageProducer).send(responseTextMessage);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    given(session.createProducer(destination)).willReturn(messageProducer);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    try {
        adapter.onMessage(sentTextMessage, session);
        fail("expected CouldNotSendReplyException with JMSException");
    } catch (ReplyFailureException ex) {
        assertEquals(JMSException.class, ex.getCause().getClass());
    }
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 12 with QueueSession

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

the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage.

@Test
public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
    QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
    QueueConnection con = mock(QueueConnection.class);
    QueueSession txSession = mock(QueueSession.class);
    QueueSession nonTxSession = mock(QueueSession.class);
    given(cf.createQueueConnection()).willReturn(con);
    given(con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
    given(txSession.getTransacted()).willReturn(true);
    given(con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createQueueConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.rollback();
    session1.close();
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close();
    con1.start();
    QueueConnection con2 = scf.createQueueConnection();
    Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close();
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    // should lead to rollback
    session2.close();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(txSession).rollback();
    verify(txSession).close();
    verify(nonTxSession).close();
    verify(con).start();
    verify(con).stop();
    verify(con).close();
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueConnectionFactory(javax.jms.QueueConnectionFactory) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) QueueSession(javax.jms.QueueSession) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 13 with QueueSession

use of javax.jms.QueueSession in project wildfly by wildfly.

the class MDBRoleTestCase method testIsMDBinRole.

@Test
public void testIsMDBinRole() throws NamingException, JMSException {
    final InitialContext ctx = new InitialContext();
    final QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
    final QueueConnection connection = factory.createQueueConnection();
    connection.start();
    final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    final Queue replyDestination = session.createTemporaryQueue();
    final QueueReceiver receiver = session.createReceiver(replyDestination);
    final Message message = session.createTextMessage("Let's test it!");
    message.setJMSReplyTo(replyDestination);
    final Destination destination = (Destination) ctx.lookup("queue/myAwesomeQueue");
    final MessageProducer producer = session.createProducer(destination);
    producer.send(message);
    producer.close();
    final Message reply = receiver.receive(TimeoutUtil.adjust(5000));
    assertNotNull(reply);
    final String result = ((TextMessage) reply).getText();
    assertEquals(SimpleSLSB.SUCCESS, result);
    connection.close();
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueReceiver(javax.jms.QueueReceiver) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 14 with QueueSession

use of javax.jms.QueueSession in project wildfly by wildfly.

the class MDBLifecycleCallback method sendReply.

private void sendReply(Queue destination, String messageID, Exception e) throws JMSException {
    QueueConnection conn = qFactory.createQueueConnection();
    try {
        QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(destination);
        ObjectMessage message = session.createObjectMessage(e == null ? "SUCCESS" : e);
        message.setJMSCorrelationID(messageID);
        sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 500);
    } finally {
        conn.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) ObjectMessage(javax.jms.ObjectMessage) QueueSession(javax.jms.QueueSession)

Example 15 with QueueSession

use of javax.jms.QueueSession in project wildfly by wildfly.

the class QueueTestMDB method sendReply.

private void sendReply(Queue destination, String messageID) throws JMSException {
    QueueConnection conn = qFactory.createQueueConnection();
    try {
        QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(destination);
        TextMessage message = session.createTextMessage("SUCCESS");
        message.setJMSCorrelationID(messageID);
        sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 500);
    } finally {
        conn.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Aggregations

QueueSession (javax.jms.QueueSession)19 QueueConnection (javax.jms.QueueConnection)16 TextMessage (javax.jms.TextMessage)16 Message (javax.jms.Message)10 Test (org.junit.Test)9 MessageProducer (javax.jms.MessageProducer)7 Queue (javax.jms.Queue)7 QueueSender (javax.jms.QueueSender)7 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 Destination (javax.jms.Destination)5 ObjectMessage (javax.jms.ObjectMessage)5 QueueReceiver (javax.jms.QueueReceiver)5 InitialContext (javax.naming.InitialContext)4 BytesMessage (javax.jms.BytesMessage)3 JMSException (javax.jms.JMSException)3 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 List (java.util.List)1