Search in sources :

Example 81 with TextMessage

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

the class MappingJackson2MessageConverterTests method fromTextMessageAsObject.

@Test
public void fromTextMessageAsObject() throws Exception {
    TextMessage textMessageMock = mock(TextMessage.class);
    Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
    String text = "{\"foo\":\"bar\"}";
    given(textMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
    given(textMessageMock.getText()).willReturn(text);
    Object result = converter.fromMessage(textMessageMock);
    assertEquals("Invalid result", result, unmarshalled);
}
Also used : TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 82 with TextMessage

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

the class MappingJackson2MessageConverterTests method testToTextMessageWithReturnType.

private void testToTextMessageWithReturnType(MethodParameter returnType) throws JMSException, NoSuchMethodException {
    converter.setTargetType(MessageType.TEXT);
    TextMessage textMessageMock = mock(TextMessage.class);
    MyAnotherBean bean = new MyAnotherBean("test", "lengthy description");
    given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
    converter.toMessage(bean, sessionMock, returnType);
    verify(textMessageMock).setStringProperty("__typeid__", MyAnotherBean.class.getName());
}
Also used : TextMessage(javax.jms.TextMessage)

Example 83 with TextMessage

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

the class MarshallingMessageConverterTests method toTextMessage.

@Test
public void toTextMessage() throws Exception {
    converter.setTargetType(MessageType.TEXT);
    TextMessage textMessageMock = mock(TextMessage.class);
    Object toBeMarshalled = new Object();
    given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
    converter.toMessage(toBeMarshalled, sessionMock);
    verify(marshallerMock).marshal(eq(toBeMarshalled), isA(Result.class));
}
Also used : TextMessage(javax.jms.TextMessage) Result(javax.xml.transform.Result) Test(org.junit.Test)

Example 84 with TextMessage

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

the class MessagingMessageConverterTests method customPayloadConverter.

@Test
public void customPayloadConverter() throws JMSException {
    TextMessage jmsMsg = new StubTextMessage("1224");
    this.converter.setPayloadConverter(new TestMessageConverter());
    Message<?> msg = (Message<?>) this.converter.fromMessage(jmsMsg);
    assertEquals(1224L, msg.getPayload());
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(javax.jms.TextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) Test(org.junit.Test)

Example 85 with TextMessage

use of javax.jms.TextMessage in project sling by apache.

the class JMSQueueManager method add.

/**
     * Add a message to the queue. The message is added to the queue transactionally and auto acknowledged.
     * @param name the name of the queue.
     * @param message the message to post to the queue.
     */
@Override
public void add(@Nonnull Types.QueueName name, @Nonnull Map<String, Object> message) {
    Session session = null;
    try {
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //TODO Instead of copy do addition at JSON writer level
        Map<String, Object> msgCopy = new HashMap<>(message);
        // set the number of retries to 0.
        msgCopy.put(NRETRIES, 0L);
        TextMessage textMessage = session.createTextMessage(Json.toJson(msgCopy));
        textMessage.setJMSType(JMSMessageTypes.JSON.toString());
        LOGGER.info("Sending to {} message {} ", name, textMessage);
        session.createProducer(session.createQueue(name.toString())).send(textMessage);
        session.commit();
        session.close();
    } catch (JMSException e) {
        LOGGER.error("Unable to send message to queue " + name, e);
        close(session);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

TextMessage (javax.jms.TextMessage)231 Test (org.junit.Test)92 Session (javax.jms.Session)75 MessageProducer (javax.jms.MessageProducer)71 Message (javax.jms.Message)70 JMSException (javax.jms.JMSException)64 Connection (javax.jms.Connection)44 Destination (javax.jms.Destination)44 MessageConsumer (javax.jms.MessageConsumer)44 ObjectMessage (javax.jms.ObjectMessage)25 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)20 StubTextMessage (org.springframework.jms.StubTextMessage)18 ConnectionFactory (javax.jms.ConnectionFactory)13 QueueConnection (javax.jms.QueueConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 MapMessage (javax.jms.MapMessage)11 Topic (javax.jms.Topic)11 JMSContext (javax.jms.JMSContext)9