Search in sources :

Example 21 with Message

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

the class AbstractAdaptableMessageListener method handleResult.

/**
	 * Handle the given result object returned from the listener method,
	 * sending a response message back.
	 * @param result the result object to handle (never {@code null})
	 * @param request the original request message
	 * @param session the JMS Session to operate on (may be {@code null})
	 * @throws ReplyFailureException if the response message could not be sent
	 * @see #buildMessage
	 * @see #postProcessResponse
	 * @see #getResponseDestination
	 * @see #sendResponse
	 */
protected void handleResult(Object result, Message request, Session session) {
    if (session != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Listener method returned result [" + result + "] - generating response message for it");
        }
        try {
            Message response = buildMessage(session, result);
            postProcessResponse(request, response);
            Destination destination = getResponseDestination(request, response, session, result);
            sendResponse(session, destination, response);
        } catch (Exception ex) {
            throw new ReplyFailureException("Failed to send reply with payload [" + result + "]", ex);
        }
    } else {
        // No JMS Session available
        if (logger.isWarnEnabled()) {
            logger.warn("Listener method returned result [" + result + "]: not generating response message for it because of no JMS Session given");
        }
    }
}
Also used : Destination(javax.jms.Destination) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) InvalidDestinationException(javax.jms.InvalidDestinationException) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException)

Example 22 with Message

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

the class MappingJackson2MessageConverter method toMessage.

protected Message toMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException {
    Message message;
    try {
        switch(this.targetType) {
            case TEXT:
                message = mapToTextMessage(object, session, objectWriter);
                break;
            case BYTES:
                message = mapToBytesMessage(object, session, objectWriter);
                break;
            default:
                message = mapToMessage(object, session, objectWriter, this.targetType);
        }
    } catch (IOException ex) {
        throw new MessageConversionException("Could not map JSON object [" + object + "]", ex);
    }
    setTypeIdOnMessage(object, message);
    return message;
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) IOException(java.io.IOException)

Example 23 with Message

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

the class JmsTemplate method doSend.

/**
	 * Send the given JMS message.
	 * @param session the JMS Session to operate on
	 * @param destination the JMS Destination to send to
	 * @param messageCreator callback to create a JMS Message
	 * @throws JMSException if thrown by JMS API methods
	 */
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    Assert.notNull(messageCreator, "MessageCreator must not be null");
    MessageProducer producer = createProducer(session, destination);
    try {
        Message message = messageCreator.createMessage(session);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + message);
        }
        doSend(producer, message);
        // Check commit - avoid commit call within a JTA transaction.
        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            // Transacted session created by this template -> commit.
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}
Also used : Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer)

Example 24 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 25 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)

Aggregations

Message (javax.jms.Message)191 TextMessage (javax.jms.TextMessage)91 Test (org.junit.Test)84 Session (javax.jms.Session)73 JMSException (javax.jms.JMSException)59 MessageProducer (javax.jms.MessageProducer)53 ObjectMessage (javax.jms.ObjectMessage)46 MessageConsumer (javax.jms.MessageConsumer)40 Connection (javax.jms.Connection)38 BytesMessage (javax.jms.BytesMessage)31 Destination (javax.jms.Destination)27 MapMessage (javax.jms.MapMessage)25 Queue (javax.jms.Queue)20 QueueSession (javax.jms.QueueSession)17 ConnectionFactory (javax.jms.ConnectionFactory)16 MessageCreator (org.springframework.jms.core.MessageCreator)12 MessageListener (javax.jms.MessageListener)9 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)9 Map (java.util.Map)8 QueueConnection (javax.jms.QueueConnection)8