2017-04-20 35 views
1

我有两个应用程序,第一个应用程序中设置了操作,如果设置了操作,我必须将其数据发送到REST Web服务并在第二个应用程序内启动一个方法。这个问题我不知道用户何时要在第一个操作中设置一个操作,所以我应该总是发送请求到REST来查看是否设置了操作。在使用REST Web服务修改时沟通2个应用程序

在第一应用

@RequestMapping("/add") 
public void setSomething(){ 
    final String uri = "http://localhost:8080/from"; 
    Greeting greeting = new Greeting(3,"Send to the other App"); 
    System.out.println("======================>>"+greeting.getContent()); 
    RestTemplate restTemplate = new RestTemplate() ; 
    Greeting result= restTemplate.postForObject(uri,greeting, Greeting.class); 
} 
在第二应用

@RequestMapping(value = "/from", method = RequestMethod.POST) 
public ResponseEntity<String> createEmployee(@RequestBody Greeting greeting) 
{ 

    return new ResponseEntity(HttpStatus.ACCEPTED); 
} 

它的工作的问候时,我的问候换另一另一个值发送到其他应用程序,但问题应用始终记住第一个值,因此不会进行任何修改

+0

没有这个例子,我认为没有人会明白你想要什么。请放一些代码,例子等 – ProgrammerBoy

+0

看起来你需要一个队列在这里。 app1侦听app2是否在队列中放置了一些结果。 – StanislavL

+0

我编辑了问题,app2在app1设置操作app2时应该从REst检索数据并启动一个方法doSomething() – Ilyasse

回答

1

“当用户要在第一个操作中设置操作时”。发生这种情况时,第一个操作中的代码被称为正确的?那么,为什么不在第二个应用程序中调用Rest端点来触发您需要的过程。

Application 1 : 

If(operationSet){ 
SendRequestToREST(operation) 
notifyApp2()// if your app 2 has rest endpoint you can use RestTemplate 
} 

Application 2 : 

ListenToRest(); 
if(operationIsSet){ 
doSomething() 
} 

或者,如果你的架构包括2级以上的应用程序。请告诉我们,我们将发明消息队列AmazonSNS免费服务的其他解决方案。但是,如果它仅用于2个实例之间的通信 - 此解决方案就足够了

您问到第一个app1如何通知第二个app2。您提议app2检查app1是否设置了参数。但现在使用该解决方案,您可以在需要时通知App2。

//I am app1 
public void notifyApp2(){ 
    final String uri = "http://localhost:8080/from"; 
    Greeting greeting = new Greeting(3,"Send to the other App"); 
    System.out.println("======================>>"+greeting.getContent()); 
    RestTemplate restTemplate = new RestTemplate() ; 
    Greeting result= restTemplate.postForObject(uri,greeting, Greeting.class); 
} 


    @RequestMapping(value = "/from", method = RequestMethod.POST) 
public ResponseEntity<String> createEmployee() 
{ 
    //I am app2 and someone is calling my rest. I am being notified. 
    //maybe they notify me to set my operations setOperation=true; 
return new ResponseEntity(HttpStatus.ACCEPTED); 

}

,现在让假设用户与APP1干活。当你从app1调用方法setOperationInApp1时,你只需在下一行调用notifyApp2()。并且您将在两个应用程序上设置操作