Search in sources :

Example 71 with Message

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

the class JmsInvokerServiceExporter method writeRemoteInvocationResult.

/**
	 * Send the given RemoteInvocationResult as a JMS message to the originator.
	 * @param requestMessage current request message
	 * @param session the JMS Session to use
	 * @param result the RemoteInvocationResult object
	 * @throws javax.jms.JMSException if thrown by trying to send the message
	 */
protected void writeRemoteInvocationResult(Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {
    Message response = createResponseMessage(requestMessage, session, result);
    MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
    try {
        producer.send(response);
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}
Also used : Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer)

Example 72 with Message

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

the class MappingJackson2MessageConverter method toMessage.

@Override
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
    Message message;
    try {
        switch(this.targetType) {
            case TEXT:
                message = mapToTextMessage(object, session, this.objectMapper.writer());
                break;
            case BYTES:
                message = mapToBytesMessage(object, session, this.objectMapper.writer());
                break;
            default:
                message = mapToMessage(object, session, this.objectMapper.writer(), 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 73 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 74 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 75 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)

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