日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 4 + Reactor Integration Example--转

發布時間:2025/4/5 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 4 + Reactor Integration Example--转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://www.concretepage.com/spring-4/spring-4-reactor-integration-example

Reactor is a framework to make event driven programming much easier. This is based on Reactor Design Pattern. Reactor is good for asynchronous applications on the JVM. Here we will create asynchronous and event driven application using Spring 4 and Reactor. Reactor uses Selectors, Consumers and Events as core module. Consumer is event consumer which needs to be notified for the event. Reactor is event gateway where event consumers are registered with a notification key. Selector is an abstraction to find consumer by invoking event. Find the example for detailed understanding.

Software Required to Run Example

To run the example we need the following software.?
1. JDK 6?
2. Gradle?
3. Eclipse

Project Structure in Eclipse

Find our demo project structure in eclipse.

?

Java Class for JSON

For the event driven example, we have created sample page that will return the JSON as below.

{"result":"success","employee":{"id":1,"name":"Ram"}}

To get this output we are using local URL as?http://localhost:8080/empdata.jsp
To consume the JSON, we have two classes as below.?
Employee.java

package com.concretepage.reactor; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown=true) public class Employee { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

Find the EmployeeResource.java.?
EmployeeResource.java

package com.concretepage.reactor; public class EmployeeResource { private String result; private Employee employee; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } }

Create Consumer using reactor.function.Consumer

To create the consumer, our class must implement reactor.function.Consumer. We need to override?accept?method which has the argument as reactor.event.Event. To get the event data we can use the method as?Event.getData. Here in this method, we are accessing a REST URL that will return the JSON data.?
MessageConsumer.java

package com.concretepage.reactor; import java.util.concurrent.CountDownLatch; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.client.RestTemplate; import reactor.event.Event; import reactor.function.Consumer; public class MessageConsumer implements Consumer<Event<Integer>> { @Autowired CountDownLatch latch; RestTemplate restTemplate = new RestTemplate(); @Override public void accept(Event<Integer> event) { EmployeeResource empResource = restTemplate.getForObject("http://localhost:8080/empdata.jsp", EmployeeResource.class); System.out.println("Employee " + event.getData() + ":" + empResource.getEmployee().getName()); latch.countDown(); } }

Create Producer

In the producer class, we are notifying consumer with a key and an event that is ready to be processed.?
MessageProducer.java

package com.concretepage.reactor; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import org.springframework.beans.factory.annotation.Autowired; import reactor.core.Reactor; import reactor.event.Event; public class MessageProducer { @Autowired Reactor reactor; @Autowired CountDownLatch latch; public void publishEmployee(int numberOfEmp) throws InterruptedException { AtomicInteger counter = new AtomicInteger(1); for (int i=0; i < numberOfEmp; i++) { reactor.notify("employees", Event.wrap(counter.getAndIncrement())); } latch.await(); System.out.println("-------Done-------"); } }

Configuration Class for Reactor

We need to create a reactor.core.Reactor bean that needs reactor.core.Environment as an argument.?
ReactorConfig.java

package com.concretepage.reactor; import java.util.concurrent.CountDownLatch; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.core.Environment; import reactor.core.Reactor; import reactor.core.spec.Reactors; @Configuration @EnableAutoConfiguration public class ReactorConfig { private static final int NUMBER_OF_EMP = 5; @Bean Environment env() { return new Environment(); } @Bean Reactor reactor(Environment env) { return Reactors.reactor() .env(env) .dispatcher(Environment.THREAD_POOL) .get(); } @Bean MessageConsumer consumer(){ return new MessageConsumer(); } @Bean MessageProducer producer(){ return new MessageProducer(); } @Bean public CountDownLatch latch() { return new CountDownLatch(NUMBER_OF_EMP); } }

Main Class to Run Example

Find the main class to run the example. Reactor provides Selectors to run our asynchronous and event-driven application. Here we using $ selector.?Reactor.on?method registers a consumer that is triggered when notification matches the given selector.?
Main.java

package com.concretepage.reactor; import static reactor.event.selector.Selectors.$; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import reactor.core.Reactor; public class Main { public static void main(String[] args) throws InterruptedException { final int NUMBER_OF_EMP = 5; AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ReactorConfig.class); ctx.refresh(); Reactor reactor = (Reactor)ctx.getBean("reactor"); MessageConsumer consumer = (MessageConsumer)ctx.getBean("consumer"); MessageProducer publisher = (MessageProducer)ctx.getBean("producer"); reactor.on($("employees"), consumer); publisher.publishEmployee(NUMBER_OF_EMP); } }

Find the output.?

Employee 2:Ram Employee 4:Ram Employee 3:Ram Employee 5:Ram Employee 1:Ram -------Done-------

Gradle for Spring and Reactor JAR Depedency

Find the gradle file to resolve the JAR dependency.?
build.gradle

apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1.0-SNAPSHOT' repositories { maven { url "https://repo.spring.io/libs-release" } mavenLocal() mavenCentral() } dependencies { compile 'org.springframework.boot:spring-boot-starter:1.2.0.RELEASE' compile 'org.projectreactor.spring:reactor-spring-context:1.1.3.RELEASE' compile 'org.springframework:spring-web:4.1.3.RELEASE' compile 'com.fasterxml.jackson.core:jackson-databind:2.5.0-rc1' compile 'org.springframework.data:spring-data-commons:1.9.1.RELEASE' compile 'org.springframework.boot:spring-boot-starter-security:1.2.0.RELEASE' }

Now we are done. Enjoy Learning.

轉載于:https://www.cnblogs.com/davidwang456/p/5690525.html

總結

以上是生活随笔為你收集整理的Spring 4 + Reactor Integration Example--转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。