当前位置: 移动技术网 > IT编程>开发语言>Java > Java系统中拆分同步和异步详解

Java系统中拆分同步和异步详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

很多开发人员说,将应用程序切换到异步处理很复杂。因为他们有一个天然需要同步通信的web应用程序。在这篇文章中,我想介绍一种方法来达到异步通信的目的:使用一些众所周知的库和工具来设计他们的系统。 下面的例子是用java编写的,但我相信它更多的是基本原理,同一个应用程序可以用任何语言来重新写。

所需的工具和库:

  • spring boot
  • rabbitmq

1.web应用程序

一个用spring mvc编写的web应用程序并运行在tomcat上。 它所做的只是将一个字符串发送到一个队列中 (异步通信的开始) 并等待另一个队列中的消息作为http响应发送回来。

首先,我们需要定义几个依赖项,然后等待spring boot执行所有必要的自动配置。

<dependencies>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-web</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-amqp</artifactid>
 </dependency>
 <dependency>
  <groupid>com.thedeanda</groupid>
  <artifactid>lorem</artifactid>
 </dependency>
</dependencies>
@springbootapplication
public class blockingapplication {
 public static void main(string[] args) {
  springapplication.run(blockingapplication.class, args);
 }
 @restcontroller
 public static class messagecontroller {
  private final rabbittemplate rabbittemplate;
  public messagecontroller(cachingconnectionfactory connectionfactory) {
   this.rabbittemplate = new rabbittemplate(connectionfactory);
  }
  @getmapping("invoke")
  public string sendmessage() {
   message response = rabbittemplate.sendandreceive("uppercase", null, request());
   return new string(response.getbody());
  }
  private static message request() {
   lorem lorem = loremipsum.getinstance();
   string name = lorem.getfirstname() + " " + lorem.getlastname();
   return new message(name.getbytes(), new messageproperties());
  }
 }
 @bean
 public cachingconnectionfactory connectionfactory() {
  cachingconnectionfactory factory = new cachingconnectionfactory();
  factory.setaddresses("localhost:5672");
  factory.setusername("admin");
  factory.setpassword("admin");
  return factory;
 }
}

2.消费端应用程序

第二个应用程序仅仅是一个等待消息的rabbitmq的消费端,将拿到的字符串转换为大写,然后将此结果发送到输出队列中。

<dependencies>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-amqp</artifactid>
 </dependency>
</dependencies>
@springbootapplication
public class serviceapplication {
 public static void main(string[] args) {
  springapplication.run(serviceapplication.class, args);
 }
 public static class messagelistener {
  public string handlemessage(byte[] message) {
   random rand = new random();
   // obtain a number between [0 - 49] + 50 = [50 - 99]
   int n = rand.nextint(50) + 50;
   string content = new string(message);
   try {
    thread.sleep(n);
   } catch (interruptedexception e) {
    e.printstacktrace();
   }
   return content.touppercase();
  }
 }
 @bean
 public cachingconnectionfactory connectionfactory() {
  cachingconnectionfactory factory = new cachingconnectionfactory();
  factory.setaddresses("localhost:5672");
  factory.setusername("admin");
  factory.setpassword("admin");
  return factory;
 }
 @bean
 public simplemessagelistenercontainer servicelistenercontainer() {
  simplemessagelistenercontainer container = new simplemessagelistenercontainer();
  container.setconnectionfactory(connectionfactory());
  container.setconcurrentconsumers(20);
  container.setmaxconcurrentconsumers(40);
  container.setqueuenames("uppercase_messages");
  container.setmessagelistener(new messagelisteneradapter(new messagelistener()));
  return container;
 }
}

3.底层如何执行的?

程序启动并首次调用sendmessage()方法后,我们可以看到spring amqp支持自动创建了一个新的回复队列并等待来自我们的服务应用程序的响应。

2019-05-12 17:23:21.451  info 4574 --- [nio-8080-exec-1] .l.directreplytomessagelistenercontainer : container initialized for queues: [amq.rabbitmq.reply-to]
2019-05-12 17:23:21.457  info 4574 --- [nio-8080-exec-1] .l.directreplytomessagelistenercontainer : simpleconsumer [queue=amq.rabbitmq.reply-to, consumertag=amq.ctag-vf-iqd9rleuljibstbci1a identity=10e58093] started

如果我们在消费端应用程序中查看消息,我们可以看到spring自动传播有关回复队列的信息以及**相关id,**用于将其传递回web应用程序以便能够将请求和响应配对在一起。

这就是发生魔术的地方。 当然,如果您想使其更复杂,您可以在协作中包含更多服务,然后将web应用程序的最终响应放入与自动生成的队列不同的队列中, 该队列只具有正确的关联id。 另外,不要忘记设置合理的超时。

这个解决方案还有一个很大的缺点 - 应用程序吞吐量。 我故意这样做,以便我可以跟进这篇文章,进一步深入调查asyncprofiler! 但是目前,我们使用tomcat作为主http服务器,默认为200个线程,这意味着我们的应用程序无法同时处理200多条消息,因为我们的服务器线程正在等待rabbitmq 回复队列的响应,直到有消息进入或发生超时。

感谢您阅读本文,敬请关注后续内容! 如果您想自己尝试一下,请查看我的github存储库

原文链接:…

作者:petr bouda

译者:keepgoingpawn

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网