JMS를 사용하는 두 개의 프로젝트, 프로젝트 A와 B를 생성하라는 요청을 받았습니다. B는 두 개의 날짜 (시작 날짜-종료 날짜)를 입력하는 양식이 포함 된 간단한 페이지가있는 웹 프로젝트입니다. 양식을 제출 한 후 B는 A에게 두 날짜를 기준으로 작업 처리 (파일 쿼리)를 요청합니다. 그러면 B는 웹 페이지에 결과를 표시합니다. 주의 사항은 jms에서이 작업을 수행해야한다는 것입니다.
내 B 컨트롤러에서 이것에 대해 생각하고 있습니다 (간체. 저는 spring을 사용하고 있습니다)
@controller
Mycontroller{
MyMessageProducer mp;
@RequestMapping(....)
public String(...){
mp.sendMessage(...);
//wait for the response here and render?
}
}
이제 프로젝트 A를 구현하는 방법에 갇혀 있습니다. 지점 간 메시징 (큐 사용)을 사용하는 경우 A는 대기열에서 메시지를 명시 적으로 가져와야합니다 (activemq 사용). A가 요청을 자동으로 수신해야하기 때문에 안타깝지 않습니까? 그러나 게시자-구독자를 사용하면이 경우 게시자는 B, 클라이언트 (B가 A에게 메시지를 보내기 때문에)가 될 것입니다. 이것은 나쁜 해결책이라고 생각합니다. 어떤 전략을 사용해야합니까?
이제 A가 메시지를 성공적으로 수신하고 파일을 쿼리한다고 가정합니다. B가 웹 페이지에 결과를 표시 할 수 있도록 결과를 B에 다시 보내려면 어떻게해야합니까? 이것을 할 방법이 있습니까?
(PS 나는 이미 튜토리얼을 기반으로 간단한 생산자와 수신기를 구현했지만 JMS를 처음 사용합니다)
가장 쉬운 방법 인 IMO 는 게이트웨이 개념을 가진 Spring Integration 을 사용 하는 것입니다 ( Enterprise Integration Patterns 참조 ). 반환 값이있는 메서드가있는을 지정할 수 있습니다 . 같은 것service-interface
public interface MessageProducer {
@Gateway
public String sendMessageAndGetReply(String name);
}
인터페이스의 프록시가 생성됩니다. <int:gateway>
요소 에서 참조 할 때 . 같은 것
<int:channel id="requestChannel"/>
<int:channel id="replyChannel"/>
<int:gateway id="messageProducerGateway" default-request-channel="requestChannel"
default-reply-channel="replyChannel"
service-interface="demo.MessageProducer">
</int:gateway>
<int-jms:outbound-gateway id="outboundJmsGateway"
connection-factory="connectionFactory" reply-channel="replyChannel"
request-channel="requestChannel" reply-destination-name="reply.queue"
request-destination-name="request.queue">
</int-jms:outbound-gateway>
이 <int-jms:outbound-gateway>
는 JMS 큐 밖으로 메시지를 보내고 답장을 받게됩니다. connectionFactory
평범한 ConnectionFactory
인스턴스입니다 (예 : ActiveMQConnectionFactory
, CachingConnectionFactory
)
jms 상호 작용의 "서버"측에서 <int-jms:inbound-gateway>
, Something like
<int-jms:inbound-gateway id="inboundJmsGateway"
request-channel="requestChannel" acknowledge="client"
connection-factory="connectionFactory" request-destination-name="request.queue" />
<int:service-activator id="messageHandler" ref="serverMessageHandler"
input-channel="requestChannel">
</int:service-activator>
은 serverMessageHandler
처리하고 게이트웨이에 대한 응답을 다시 반환하는 방법으로 단순한 구성 요소입니다.
@Component
public class ServerMessageHandler {
@ServiceActivator
public String handleMessage(String message) {
return "Hello, " + message;
}
}
간단한 데모 실행
AbstractApplicationContext client = new ClassPathXmlApplicationContext("demo-gateway.xml");
AbstractApplicationContext server = new ClassPathXmlApplicationContext("demo-gateway-server.xml");
MessageProducer producer = client.getBean(MessageProducer.class);
String returnedMessage = producer.sendMessageAndGetReply("StackOverflow");
System.out.println(returnedMessage);
당신은 얻는다 "Hello, StackOverflow"
. 프레임 워크에 대한 기본적인 이해를하게되면 그다지 많지 않습니다. 동기식 예제를 제공 한 예제입니다. github의 spring-integration-samples 에서 전체 예제 (다른 예제와 함께)를 볼 수 있습니다 . 기본 사항에 익숙해지기 위해 참조 가이드를 살펴 보는 데 시간이 걸립니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다