Spring Reactor multithreaded Configuration

Source: Internet
Author: User

1: The introduction of the jar package, which is used by MAVEN, only need to reference a jar package on the line

<DEPENDENCY>

<GROUPID>org.projectreactor</GROUPID>

Reactor-spring</artifactid>

<VERSION>1.0.0.RELEASE</VERSION>

</DEPENDENCY>

2: Write a reactor configured bean

@Configuration

@EnableReactor

public class Reactorconfig {

@Bean (name = "Rootreactor")

Public Reactor rootreactor (Environment env) {

Return Reactors.reactor (). ENV (ENV). Get ();

}

@Bean (name = "Reportreactor")

Public Reactor reportreactor (Environment env) {

Return Reactors.reactor (). ENV (ENV). Get ();

}

}

3: The handling of the event class, generally with hander end, convenient to distinguish:

@Component

public class Indexhandler {

@Autowired

@Qualifier ("Rootreactor")

Private Reactor Reactor;

@Selector (value = "Hello", reactor = "@rootReactor")

public void Handletesttopic (event<string> evt) throws Exception {

System.out.println ("************");

}

}

4: Finally, in the controller or service to notify the new thread:

@Controller

public class Indexcontroller {

@Autowired

@Qualifier ("Rootreactor")

Private Reactor R;

@RequestMapping ("Chen")

@Transactional

public void Chen () {

R.notify ("Hello", Event.wrap ("Hello"));

}

}











---------------------------------------------------------------------------------------



Transfer from: Rx (reactive Extensions) IntroductionReactor-a Foundation for Reactive fastdata appli This article briefly introduces the basic features of Spring Reactor 1.0.

Currently reactor is the following project as the Spring.io core package.

Reactor is a basic library package
– The abstraction of a gray area positioned between the user level and the low level.
– Ability to build components and application cores on reactor
– Drive server and Data integration library, Domain integration library, event-driven architecture

The application of reactor is reactive.
– Belongs to reactive Extensions in. NET
– Similar to Netflix RxJava
– Observer pattern Observer pattern

The reactor app sends events based on a single selector.
– Like a routing topic in a messaging system, but it is an object
– Support for Regex, URI template, Class.isassingablefrom, custom logic

The Reactor core internally encapsulates the ringbuffer of the Lmax disruptor and supports a variety of spring applications, such as:




Reactor Demo Code

New Environment (); Reactor Reactor = Reactors.reactor ()                               . ENV (ENV)                               . Dispatcher (Ring_buffer)                               . get (); Reactor.on ($ ("topic"), (event<string> ev) →{                             System.out.println ("Hello" + ev.getdata ());                  }); Reactor.notify ("topic", Event.wrap ("John Doe"));

Ring_buffer is disruptor ringbuffer operation, familiar with the disruptor should know.

Reactor.notify sends an event, and Reactor.on is able to accept an instant response to the event.

Reactor Dispatcher Dispatchers a akka-like dispenser
Dispatcher management tasks are performed in the following ways:
–threadpoolexecutordispatcher
The standard Threadpoolexecutor

–blockingqueuedispatcher
Ability to poll for events

–ringbufferdispatcher
LMAX disruptor Ringbuffer

–synchronousdispatcher


Reactor's selectors
Selectors is the left side of an equation.
– A selector can be created by any object using $ (obj)
(Or: Selectors.object (obj))
– A selector can release data from a matching key
–predicate<t> selectors can create a match to specific domain guidelines
(domain-specific criteria)

such as Regexselector:
Reactor.on (R ("some" (. +) "), (event<string> ev) →{
s'll be ' topic '
String s = ev.getheaders (). Get ("group1");
});

Reactor.notify ("Some.topic", Event.wrap ("John Doe"));

where R ("some" (. *) matches the event sender "Some.topic".

Uritemplateselector can match strings from URIs:
Reactor.on (U ("/some/{topic}"), (event<string> ev) →{
s'll be ' topic '
String s = ev.getheaders (). Get ("topic");
});
Reactor.notify ("/some/topic", Event.wrap ("John Doe"));

Stream of Reactor
Streams allows data-based function combinations composition
–callback++
– Similar to Netflix RxJava Observable, JDK 8 Stream

Stream<string> Str;str.map (string::touppercase)     . Filter (newpublicboolean Test (String s) {...}     })    . Consume (S→log.info ("Consumed string {}", s));


Reactor's Promise
Allow sharing of functions between stream

Promise<string> p; String s = P        . onsuccess (S→log.info ("Consumed string {}", s))        . OnFailure (T→log.error (T.getmessage (), T))        . OnComplete (T→log.info ("complete"))        . Await (5, SECONDS);p. Map (string::touppercase). Consume ("UC: {}" , s));


Reactor's Processor
Simply switch the Disruptor API to the reactor API directly
Super fast performance for #uberfastdata

Processor<buffer> proc;operation<buffer> op = proc.prepare (); Op.get (). Append (data). Flip (); Op.commit (); Proc.batch (Buff→buff.append (data). Flip ());


Integration with spring:
First use @enablereactor to activate reactor
 Public class reactorconfiguration {public  return reactors.reactor (). ENV (ENV)                  . Dispatcher (ring_ BUFFER). get ();   }    Public return Reactors.reactor (). ENV (ENV)                  . Dispatcher (Ring_buffer). get ();}


Then the listener or Observer writes:
 Public class Simplehandler {    private Reactor Reactor;     Public void //Handle data


Reactor's Groovy Integration:
@CompileStaticdef welcome () {    reactor.on (' greetings ') {String S-            reply "Hello $s" reply "How is is you            ?"} Reactor.notify ' greetings ', ' Jon '           reactor.send (' Greetings ', ' Stephane ') {                  println it            cancel ()           }}

Spring Reactor multithreaded Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.