Search in sources :

Example 6 with QueueSession

use of javax.jms.QueueSession in project oxAuth by GluuFederation.

the class ApplicationAuditLogger method loggingThroughJMS.

private void loggingThroughJMS(OAuth2AuditLog oAuth2AuditLog) {
    QueueConnection connection = null;
    try {
        connection = pooledConnectionFactory.createQueueConnection();
        connection.start();
        QueueSession session = connection.createQueueSession(transacted, ACK_MODE);
        MessageProducer producer = session.createProducer(session.createQueue(CLIENT_QUEUE_NAME));
        TextMessage txtMessage = session.createTextMessage();
        txtMessage.setText(ServerUtil.asPrettyJson(oAuth2AuditLog));
        producer.send(txtMessage);
    } catch (JMSException e) {
        log.error("Can't send message", e);
    } catch (IOException e) {
        log.error("Can't serialize the audit log", e);
    } catch (Exception e) {
        log.error("Can't send message, please check your activeMQ configuration.", e);
    } finally {
        if (connection == null)
            return;
        try {
            connection.close();
        } catch (JMSException e) {
            log.error("Can't close connection.");
        }
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) IOException(java.io.IOException) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 7 with QueueSession

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
public void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    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(null);
    TextMessage responseTextMessage = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    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 InvalidDestinationException");
    } catch (ReplyFailureException ex) {
        assertEquals(InvalidDestinationException.class, ex.getCause().getClass());
    }
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) InvalidDestinationException(javax.jms.InvalidDestinationException) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 8 with QueueSession

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException.

@Test
public void testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException() throws Exception {
    final TextMessage message = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    willThrow(new IllegalArgumentException("Doe!")).given(delegate).handleMessage(message);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    try {
        adapter.onMessage(message, session);
        fail("expected ListenerExecutionFailedException");
    } catch (ListenerExecutionFailedException ex) {
    /* expected */
    }
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 9 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 10 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)

Aggregations

QueueSession (javax.jms.QueueSession)17 QueueConnection (javax.jms.QueueConnection)14 TextMessage (javax.jms.TextMessage)14 Message (javax.jms.Message)10 Test (org.junit.Test)8 MessageProducer (javax.jms.MessageProducer)7 Queue (javax.jms.Queue)7 Destination (javax.jms.Destination)5 ObjectMessage (javax.jms.ObjectMessage)5 QueueConnectionFactory (javax.jms.QueueConnectionFactory)5 QueueReceiver (javax.jms.QueueReceiver)5 QueueSender (javax.jms.QueueSender)5 BytesMessage (javax.jms.BytesMessage)3 JMSException (javax.jms.JMSException)3 InitialContext (javax.naming.InitialContext)3 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Connection (javax.jms.Connection)1 InvalidDestinationException (javax.jms.InvalidDestinationException)1