Search in sources :

Example 86 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
public void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    Queue destination = mock(Queue.class);
    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...
    // we want to fall back to the default...
    given(sentTextMessage.getJMSReplyTo()).willReturn(null);
    TextMessage responseTextMessage = mock(TextMessage.class);
    QueueSender queueSender = mock(QueueSender.class);
    Session session = mock(Session.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    given(session.createProducer(destination)).willReturn(queueSender);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    adapter.setDefaultResponseDestination(destination);
    adapter.onMessage(sentTextMessage, session);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(queueSender).send(responseTextMessage);
    verify(queueSender).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) Test(org.junit.Test)

Example 87 with Message

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

the class JmsInvokerTests method receiveTimeoutExpired.

@Test
public void receiveTimeoutExpired() {
    JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {

        @Override
        protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
            // faking no message received
            return null;
        }
    };
    pfb.setServiceInterface(ITestBean.class);
    pfb.setConnectionFactory(this.mockConnectionFactory);
    pfb.setQueue(this.mockQueue);
    pfb.setReceiveTimeout(1500);
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    thrown.expect(RemoteTimeoutException.class);
    thrown.expectMessage("1500 ms");
    thrown.expectMessage("getAge");
    proxy.getAge();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 88 with Message

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

the class JmsInvokerTests method doTestJmsInvokerProxyFactoryBeanAndServiceExporter.

private void doTestJmsInvokerProxyFactoryBeanAndServiceExporter(boolean dynamicQueue) throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setMessageConverter(new MockSimpleMessageConverter());
    exporter.afterPropertiesSet();
    JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {

        @Override
        protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
            Session mockExporterSession = mock(Session.class);
            ResponseStoringProducer mockProducer = new ResponseStoringProducer();
            given(mockExporterSession.createProducer(requestMessage.getJMSReplyTo())).willReturn(mockProducer);
            exporter.onMessage(requestMessage, mockExporterSession);
            assertTrue(mockProducer.closed);
            return mockProducer.response;
        }
    };
    pfb.setServiceInterface(ITestBean.class);
    pfb.setConnectionFactory(this.mockConnectionFactory);
    if (dynamicQueue) {
        pfb.setQueueName("myQueue");
    } else {
        pfb.setQueue(this.mockQueue);
    }
    pfb.setMessageConverter(new MockSimpleMessageConverter());
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    proxy.setStringArray(new String[] { "str1", "str2" });
    assertTrue(Arrays.equals(new String[] { "str1", "str2" }, proxy.getStringArray()));
    try {
        proxy.exceptional(new IllegalStateException());
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    try {
        proxy.exceptional(new IllegalAccessException());
        fail("Should have thrown IllegalAccessException");
    } catch (IllegalAccessException ex) {
    // expected
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession)

Example 89 with Message

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

the class SimpleMessageConverterTests method testStringConversion.

@Test
public void testStringConversion() throws JMSException {
    Session session = mock(Session.class);
    TextMessage message = mock(TextMessage.class);
    String content = "test";
    given(session.createTextMessage(content)).willReturn(message);
    given(message.getText()).willReturn(content);
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertEquals(content, converter.fromMessage(msg));
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 90 with Message

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

the class SimpleMessageConverterTests method testByteArrayConversion.

@Test
public void testByteArrayConversion() throws JMSException {
    Session session = mock(Session.class);
    BytesMessage message = mock(BytesMessage.class);
    byte[] content = "test".getBytes();
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
    given(session.createBytesMessage()).willReturn(message);
    given(message.getBodyLength()).willReturn((long) content.length);
    given(message.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]);
        }
    });
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
    verify(message).writeBytes(content);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BytesMessage(javax.jms.BytesMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) 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