Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Last active May 23, 2018 11:25
Show Gist options
  • Save bdarfler/2485f3d5e8c169e8154d857596d8aa81 to your computer and use it in GitHub Desktop.
Save bdarfler/2485f3d5e8c169e8154d857596d8aa81 to your computer and use it in GitHub Desktop.
@Component
public class Requestor {
private static final class CorrelationIdPostProcessor implements MessagePostProcessor {
private final String correlationId;
public CorrelationIdPostProcessor( final String correlationId ) {
this.correlationId = correlationId;
}
@Override
public Message postProcessMessage( final Message msg ) throws JMSException {
msg.setJMSCorrelationID( correlationId );
return msg;
}
}
private final JmsTemplate jmsTemplate;
@Autowired
public RequestGateway( JmsTemplate jmsTemplate ) {
this.jmsTemplate = jmsTemplate;
}
public String request( final String request, String queue ) throws IOException {
final String correlationId = UUID.randomUUID().toString();
jmsTemplate.convertAndSend( queue+”.request”, request, new CorrelationIdPostProcessor( correlationId ) );
return (String) jmsTemplate.receiveSelectedAndConvert( queue+”.response”, “JMSCorrelationID=’” + correlationId + “‘“ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment