【问题标题】:Request response over HTTP with Spring and activemq使用 Spring 和 activemq 通过 HTTP 请求响应
【发布时间】:2018-01-29 15:22:06
【问题描述】:

我正在构建一个简单的 REST api,它将 Web 服务器连接到后端服务,该服务执行简单的检查并发送响应。

所以客户端(通过 HTTP)-> 到 Web 服务器(通过 ACTIVEMQ/CAMEL)-> 到 Checking-Service,然后再返回。

GET 请求的端点是 "/{id}"。我试图通过 queue:ws-outqueue:cs-in 发送一条消息,并将其重新映射回原始 GET 请求。

Checking-Service (cs) 代码很好,它只是使用 jmslistener 将 CheckMessage 对象中的值更改为 true。

我已经在网上彻底搜索了示例,但找不到任何工作。我找到的最接近的是following

这是我目前在 Web 服务器上所拥有的 (ws)。

RestController

import ...

@RestController
public class RESTController extends Exception{

    @Autowired
    CamelContext camelContext;

    @Autowired
    JmsTemplate jmsTemplate;

    @GetMapping("/{id}")
    public String testCamel(@PathVariable String id) {

        //Object used to send out
        CheckMessage outMsg = new CheckMessage(id);
        //Object used to receive response
        CheckMessage inMsg = new CheckMessage(id);

        //Sending the message out (working)
        jmsTemplate.convertAndSend("ws-out", outMsg);

        //Returning the response to the client (need correlation to the out message"
        return jmsTemplate.receiveSelectedAndConvert("ws-in", ??);
    }

}

ws 上的监听器

@Service
public class WSListener {

    //For receiving the response from Checking-Service
    @JmsListener(destination = "ws-in")
    public void receiveMessage(CheckMessage response) {
    }
}

谢谢!

【问题讨论】:

    标签: spring-boot activemq spring-jms


    【解决方案1】:
    1. 您从“ws-in”接收消息,有 2 个消费者 jmsTemplate.receiveSelectedAndConvert 和 WSListener !!来自队列的消息由 2 个中的一个使用。

    2. 您将消息发送到“ws-out”并从“ws-in”消费??最后一个队列 为空且未收到任何消息,您必须发送消息至 它

    您需要一个有效的选择器来检索带有基于 JMSCorrelationID 示例的 receiveSelectedAndConvert 的消息(例如您提到的示例)或从其余请求中收到的 id,但您需要将此 id 添加到消息头中,如下所示

        this.jmsTemplate.convertAndSend("ws-out", id, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage tm = session.createTextMessage(new CheckMessage(id));
                tm.setJMSCorrelationID(id);
                return tm;
            }
        });
    
    
        return jmsTemplate.receiveSelectedAndConvert("ws-in", "JMSCorrelationID='" + id+ "'");
    

    将消息从“ws-out”转发到“ws-in”

    @Service
    public class WSListener {
    
        //For receiving the response from Checking-Service
        @JmsListener(destination = "ws-out")
        public void receiveMessage(CheckMessage response) {
            jmsTemplate.convertAndSend("ws-in", response);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2015-12-07
      • 2018-01-12
      • 2012-06-26
      相关资源
      最近更新 更多