Search in sources :

Example 76 with Message

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

the class JmsListenerContainerFactoryIntegrationTests method parameterAnnotationWithJdkProxy.

@Test
public void parameterAnnotationWithJdkProxy() throws JMSException {
    ProxyFactory pf = new ProxyFactory(sample);
    listener = (JmsEndpointSampleInterface) pf.getProxy();
    containerFactory.setMessageConverter(new UpperCaseMessageConverter());
    MethodJmsListenerEndpoint endpoint = createDefaultMethodJmsEndpoint(JmsEndpointSampleInterface.class, "handleIt", String.class, String.class);
    Message message = new StubTextMessage("foo-bar");
    message.setStringProperty("my-header", "my-value");
    invokeListener(endpoint, message);
    assertListenerMethodInvocation("handleIt");
}
Also used : TextMessage(javax.jms.TextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) Message(javax.jms.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) StubTextMessage(org.springframework.jms.StubTextMessage) Test(org.junit.Test)

Example 77 with Message

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

the class JmsListenerContainerFactoryIntegrationTests method parameterAnnotationWithCglibProxy.

@Test
public void parameterAnnotationWithCglibProxy() throws JMSException {
    ProxyFactory pf = new ProxyFactory(sample);
    pf.setProxyTargetClass(true);
    listener = (JmsEndpointSampleBean) pf.getProxy();
    containerFactory.setMessageConverter(new UpperCaseMessageConverter());
    MethodJmsListenerEndpoint endpoint = createDefaultMethodJmsEndpoint(JmsEndpointSampleBean.class, "handleIt", String.class, String.class);
    Message message = new StubTextMessage("foo-bar");
    message.setStringProperty("my-header", "my-value");
    invokeListener(endpoint, message);
    assertListenerMethodInvocation("handleIt");
}
Also used : TextMessage(javax.jms.TextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) Message(javax.jms.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) StubTextMessage(org.springframework.jms.StubTextMessage) Test(org.junit.Test)

Example 78 with Message

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

the class SimpleMessageListenerContainerTests method testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate.

@Test
public void testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate() throws Exception {
    this.container.setSessionTransacted(true);
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(true);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            throw new UnsupportedOperationException();
        }
    });
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    // Session is rolled back 'cos it is transacted...
    verify(session).rollback();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session) Test(org.junit.Test)

Example 79 with Message

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

the class SimpleMessageListenerContainerTests method testCorrectSessionExposedForSessionAwareMessageListenerInvocation.

@Test
public void testCorrectSessionExposedForSessionAwareMessageListenerInvocation() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    final Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final Set<String> failure = new HashSet<>(1);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new SessionAwareMessageListener<Message>() {

        @Override
        public void onMessage(Message message, Session sess) {
            try {
                // Check correct Session passed into SessionAwareMessageListener.
                assertSame(sess, session);
            } catch (Throwable ex) {
                failure.add("MessageListener execution failed: " + ex);
            }
        }
    });
    this.container.afterPropertiesSet();
    this.container.start();
    final Message message = mock(Message.class);
    messageConsumer.sendMessage(message);
    if (!failure.isEmpty()) {
        fail(failure.iterator().next().toString());
    }
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) Connection(javax.jms.Connection) Session(javax.jms.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with Message

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

the class SimpleMessageListenerContainerTests method testRegisteredExceptionListenerIsInvokedOnException.

@Test
public void testRegisteredExceptionListenerIsInvokedOnException() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final JMSException theException = new JMSException(EXCEPTION_MESSAGE);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new SessionAwareMessageListener<Message>() {

        @Override
        public void onMessage(Message message, Session session) throws JMSException {
            throw theException;
        }
    });
    ExceptionListener exceptionListener = mock(ExceptionListener.class);
    this.container.setExceptionListener(exceptionListener);
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(exceptionListener).onException(theException);
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) ExceptionListener(javax.jms.ExceptionListener) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

Message (javax.jms.Message)230 TextMessage (javax.jms.TextMessage)103 Test (org.junit.Test)91 Session (javax.jms.Session)86 JMSException (javax.jms.JMSException)77 MessageProducer (javax.jms.MessageProducer)64 ObjectMessage (javax.jms.ObjectMessage)52 MessageConsumer (javax.jms.MessageConsumer)50 Connection (javax.jms.Connection)45 BytesMessage (javax.jms.BytesMessage)37 Destination (javax.jms.Destination)30 MapMessage (javax.jms.MapMessage)26 Queue (javax.jms.Queue)20 ConnectionFactory (javax.jms.ConnectionFactory)18 QueueSession (javax.jms.QueueSession)17 MessageCreator (org.springframework.jms.core.MessageCreator)15 MessageListener (javax.jms.MessageListener)12 Map (java.util.Map)10 StreamMessage (javax.jms.StreamMessage)10 TemporaryQueue (javax.jms.TemporaryQueue)9