使用 Spring 和 activemq 通过 HTTP 请求响应
Request response over HTTP with Spring and activemq
我正在构建一个简单的 REST api,它将 Web 服务器连接到后端服务,执行简单的检查并发送响应。
所以客户端(通过 HTTP)-> 到 Web 服务器(通过 ACTIVEMQ/CAMEL)-> 到检查服务,然后再返回。
GET 请求的端点是 "/{id}"。我试图让它通过 queue:ws-out 发送消息到 queue: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) {
}
}
谢谢!
您使用 2 个消费者 jmsTemplate.receiveSelectedAndConvert 和 WSListener 从 "ws-in" 接收消息!!来自 queue 的消息被 2.
之一消费
您向 "ws-out" 发送消息并从 "ws-in" 消费 ??最后 queue
为空且未收到任何消息,您必须发送消息至
它
您需要一个有效的选择器来检索基于 JMSCorrelationID 的 receiveSelectedAndConvert 消息,例如您提到的示例或从其余请求中收到的 ID,但您需要将此 ID 添加到消息 headers 中,如下所示
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);
}
}
我正在构建一个简单的 REST api,它将 Web 服务器连接到后端服务,执行简单的检查并发送响应。
所以客户端(通过 HTTP)-> 到 Web 服务器(通过 ACTIVEMQ/CAMEL)-> 到检查服务,然后再返回。
GET 请求的端点是 "/{id}"。我试图让它通过 queue:ws-out 发送消息到 queue: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) {
}
}
谢谢!
您使用 2 个消费者 jmsTemplate.receiveSelectedAndConvert 和 WSListener 从 "ws-in" 接收消息!!来自 queue 的消息被 2.
之一消费
您向 "ws-out" 发送消息并从 "ws-in" 消费 ??最后 queue 为空且未收到任何消息,您必须发送消息至 它
您需要一个有效的选择器来检索基于 JMSCorrelationID 的 receiveSelectedAndConvert 消息,例如您提到的示例或从其余请求中收到的 ID,但您需要将此 ID 添加到消息 headers 中,如下所示
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);
}
}