Message response = buildMessage(session, result); postProcessResponse(request, response); Destination destination = getResponseDestination(request, response, session, result); sendResponse(session, destination, response);
@Override protected Object extractPayload(Message message) throws JMSException { Object payload = extractMessage(message); if (message instanceof BytesMessage) { try { // In case the BytesMessage is going to be received as a user argument: // reset it, otherwise it would appear empty to such processing code... ((BytesMessage) message).reset(); } catch (JMSException ex) { // Continue since the BytesMessage typically won't be used any further. logger.debug("Failed to reset BytesMessage after payload extraction", ex); } } return payload; }
private Destination getResponseDestination(Message request, Message response, Session session, Object result) throws JMSException { if (result instanceof JmsResponse) { JmsResponse<?> jmsResponse = (JmsResponse) result; Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); if (destination != null) { return destination; } } return getResponseDestination(request, response, session); }
@Override public abstract void onMessage(Message message, @Nullable Session session) throws JMSException;
Message response = buildMessage(session, result); postProcessResponse(request, response); Destination destination = getResponseDestination(request, response, session, result); sendResponse(session, destination, response);
/** * Build a JMS message to be sent as response based on the given result object. * @param session the JMS Session to operate on * @param result the content of the message, as returned from the listener method * @return the JMS {@code Message} (never {@code null}) * @throws JMSException if thrown by JMS API methods * @see #setMessageConverter */ protected Message buildMessage(Session session, Object result) throws JMSException { Object content = preProcessResponse(result instanceof JmsResponse ? ((JmsResponse<?>) result).getResponse() : result); MessageConverter converter = getMessageConverter(); if (converter != null) { if (content instanceof org.springframework.messaging.Message) { return this.messagingMessageConverter.toMessage(content, session); } else { return converter.toMessage(content, session); } } if (!(content instanceof Message)) { throw new MessageConversionException( "No MessageConverter specified - cannot handle message [" + content + "]"); } return (Message) content; }
/** * Send the given response message to the given destination. * @param response the JMS message to send * @param destination the JMS destination to send to * @param session the JMS session to operate on * @throws JMSException if thrown by JMS API methods * @see #postProcessProducer * @see javax.jms.Session#createProducer * @see javax.jms.MessageProducer#send */ protected void sendResponse(Session session, Destination destination, Message response) throws JMSException { MessageProducer producer = session.createProducer(destination); try { postProcessProducer(producer, response); QosSettings settings = getResponseQosSettings(); if (settings != null) { producer.send(response, settings.getDeliveryMode(), settings.getPriority(), settings.getTimeToLive()); } else { producer.send(response); } } finally { JmsUtils.closeMessageProducer(producer); } }
@Override protected Message createMessageForPayload(Object payload, Session session, @Nullable Object conversionHint) throws JMSException { MessageConverter converter = getMessageConverter(); if (converter == null) { throw new IllegalStateException("No message converter, cannot handle '" + payload + "'"); } if (converter instanceof SmartMessageConverter) { return ((SmartMessageConverter) converter).toMessage(payload, session, conversionHint); } return converter.toMessage(payload, session); }
/** * Resolve the default response destination into a JMS {@link Destination}, using this * accessor's {@link DestinationResolver} in case of a destination name. * @return the located {@link Destination} * @throws javax.jms.JMSException if resolution failed * @see #setDefaultResponseDestination * @see #setDefaultResponseQueueName * @see #setDefaultResponseTopicName * @see #setDestinationResolver */ @Nullable protected Destination resolveDefaultResponseDestination(Session session) throws JMSException { if (this.defaultResponseDestination instanceof Destination) { return (Destination) this.defaultResponseDestination; } if (this.defaultResponseDestination instanceof DestinationNameHolder) { DestinationNameHolder nameHolder = (DestinationNameHolder) this.defaultResponseDestination; return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic); } return null; }
/** * Send the given response message to the given destination. * @param response the JMS message to send * @param destination the JMS destination to send to * @param session the JMS session to operate on * @throws JMSException if thrown by JMS API methods * @see #postProcessProducer * @see javax.jms.Session#createProducer * @see javax.jms.MessageProducer#send */ protected void sendResponse(Session session, Destination destination, Message response) throws JMSException { MessageProducer producer = session.createProducer(destination); try { postProcessProducer(producer, response); producer.send(response); } finally { JmsUtils.closeMessageProducer(producer); } }
@Override public abstract void onMessage(Message message, @Nullable Session session) throws JMSException;
/** * Build a JMS message to be sent as response based on the given result object. * @param session the JMS Session to operate on * @param result the content of the message, as returned from the listener method * @return the JMS {@code Message} (never {@code null}) * @throws JMSException if thrown by JMS API methods * @see #setMessageConverter */ protected Message buildMessage(Session session, Object result) throws JMSException { Object content = preProcessResponse(result instanceof JmsResponse ? ((JmsResponse<?>) result).getResponse() : result); MessageConverter converter = getMessageConverter(); if (converter != null) { if (content instanceof org.springframework.messaging.Message) { return this.messagingMessageConverter.toMessage(content, session); } else { return converter.toMessage(content, session); } } if (!(content instanceof Message)) { throw new MessageConversionException( "No MessageConverter specified - cannot handle message [" + content + "]"); } return (Message) content; }
/** * Send the given response message to the given destination. * @param response the JMS message to send * @param destination the JMS destination to send to * @param session the JMS session to operate on * @throws JMSException if thrown by JMS API methods * @see #postProcessProducer * @see javax.jms.Session#createProducer * @see javax.jms.MessageProducer#send */ protected void sendResponse(Session session, Destination destination, Message response) throws JMSException { MessageProducer producer = session.createProducer(destination); try { postProcessProducer(producer, response); QosSettings settings = getResponseQosSettings(); if (settings != null) { producer.send(response, settings.getDeliveryMode(), settings.getPriority(), settings.getTimeToLive()); } else { producer.send(response); } } finally { JmsUtils.closeMessageProducer(producer); } }
/** * Extract the message body from the given JMS message. * @param message the JMS {@code Message} * @return the content of the message, to be passed into the listener method * as an argument * @throws MessageConversionException if the message could not be extracted */ protected Object extractMessage(Message message) { try { MessageConverter converter = getMessageConverter(); if (converter != null) { return converter.fromMessage(message); } return message; } catch (JMSException ex) { throw new MessageConversionException("Could not convert JMS message", ex); } }
/** * Resolve the default response destination into a JMS {@link Destination}, using this * accessor's {@link DestinationResolver} in case of a destination name. * @return the located {@link Destination} * @throws javax.jms.JMSException if resolution failed * @see #setDefaultResponseDestination * @see #setDefaultResponseQueueName * @see #setDefaultResponseTopicName * @see #setDestinationResolver */ @Nullable protected Destination resolveDefaultResponseDestination(Session session) throws JMSException { if (this.defaultResponseDestination instanceof Destination) { return (Destination) this.defaultResponseDestination; } if (this.defaultResponseDestination instanceof DestinationNameHolder) { DestinationNameHolder nameHolder = (DestinationNameHolder) this.defaultResponseDestination; return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic); } return null; }
Message response = buildMessage(session, result); postProcessResponse(request, response); Destination destination = getResponseDestination(request, response, session, result); sendResponse(session, destination, response);
public void onMessage(Message message) { try { onMessage(message, null); handleListenerException(ex);
private Destination getResponseDestination(Message request, Message response, Session session, Object result) throws JMSException { if (result instanceof JmsResponse) { JmsResponse<?> jmsResponse = (JmsResponse) result; Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); if (destination != null) { return destination; } } return getResponseDestination(request, response, session); }
@Override protected Message createMessageForPayload(Object payload, Session session, Object conversionHint) throws JMSException { MessageConverter converter = getMessageConverter(); if (converter == null) { throw new IllegalStateException("No message converter, cannot handle '" + payload + "'"); } if (converter instanceof SmartMessageConverter) { return ((SmartMessageConverter) converter).toMessage(payload, session, conversionHint); } return converter.toMessage(payload, session); }
@Override protected Object extractPayload(Message message) throws JMSException { Object payload = extractMessage(message); if (message instanceof BytesMessage) { try { // In case the BytesMessage is going to be received as a user argument: // reset it, otherwise it would appear empty to such processing code... ((BytesMessage) message).reset(); } catch (JMSException ex) { // Continue since the BytesMessage typically won't be used any further. logger.debug("Failed to reset BytesMessage after payload extraction", ex); } } return payload; }