Spring Integrated Disruptor3

Source: Internet
Author: User

First, what is Disruptor

Functionally, disruptor is the function of "queue", and it is a bounded queue. Then its application scenario is naturally a "producer-consumer" model of application.

You can make a simple comparison with the blockingqueue of the JDK to get a better understanding of what disruptor is.

We know that Blockingqueue is a FIFO queue where the consumer (Consumer) can be notified when a producer (Producer) publishes (publish) an event (or "message") to the queue, and if there is no event, the consumer is blocked, Until a new event is released by the producer.

These are what disruptor can do, and the difference is that disruptor can do more:

    • The same "event" can have multiple consumers, consumers can be both parallel processing, but also can rely on each other to form a processing sequence (to form a dependency graph);
    • Pre-allocated memory space for storing event content;
    • Extreme optimization and lock-free design for extremely high performance objectives;

While the above simply describes what disruptor is, it is not so clear as to what it "can do". In short, when you need to exchange data between two separate processes, you can use Disruptor. Of course, using queues is also possible, but disruptor performance is better.

Second, actual combat

This article does not elaborate on the concrete principle of disruptor's work, but simply integrates spring with it. The integration process is simple, with the following steps:

1. Introduction of disruptor in POM file

<dependency>    <groupId>com.lmax</groupId>    <artifactid>disruptor</artifactid >    <version>3.4.2</version></dependency>

2. Create an Event

@Data  Public class notifyevent {    private  String message;}

3. Create message factory for production messages

 Public class Implements eventfactory {    @Override    public  Object newinstance () {        return  New  notifyevent ();    }}

4. Create consumer, here to handle business logic

 Public classNotifyeventhandlerImplementsEventhandler<notifyevent>,workhandler<notifyevent>{@Override Public voidOnEvent (Notifyevent notifyevent,LongLBooleanbthrowsException {System.out.println ("Receive Message");  This. OnEvent (notifyevent); } @Override Public voidOnEvent (notifyevent notifyevent)throwsException {System.out.println (notifyevent+ ">>>" +Uuid.randomuuid (). toString ()); }}

5. Custom Exceptions

@Log4j2 Public classNotifyeventhandlerexceptionImplementsExceptionhandler {@Override Public voidHandleeventexception (Throwable throwable,Longsequence, Object event)        {Throwable.fillinstacktrace (); Log.error ("Process data error sequence ==[{}] event==[{}], ex ==[{}]", Sequence, event.tostring (), Throwable.getmessage ()); } @Override Public voidhandleonstartexception (Throwable throwable) {log.error ("Start disruptor error ==[{}]!", Throwable.getmessage ()); } @Override Public voidhandleonshutdownexception (Throwable throwable) {log.error ("Shutdown disruptor error ==[{}]!", Throwable.getmessage ()); }}

6. Integrate spring to initialize the disruptor

@Service Public classNotifyserviceimplImplementsInotifyservice, Disposablebean,initializingbean {PrivateDisruptor<notifyevent>disruptor; Private Static Final intRing_buffer_size = 1024 * 1024; @Override Public voidDestroy ()throwsException {disruptor.shutdown (); } @Override Public voidAfterpropertiesset ()throwsException {disruptor=NewDisruptor<notifyevent> (NewNotifyeventfactory (), Ring_buffer_size, Executors.defaultthreadfactory (), Producertype.single,Newblockingwaitstrategy ()); Disruptor.setdefaultexceptionhandler (Newnotifyeventhandlerexception ()); Disruptor.handleeventswith (NewNotifyeventhandler ());    Disruptor.start (); } @Override Public voidsendnotify (String message) {Ringbuffer<NotifyEvent> Ringbuffer =Disruptor.getringbuffer ();//ringbuffer.publishevent (New eventtranslatoronearg<notifyevent, string> () {//@Override//Public void Translateto (Notifyevent event, long sequence, String data) {//event.setmessage (data);//            }//}, message);Ringbuffer.publishevent (event, sequence, data)event.setmessage (data), message); Lambda notation, if you use a section of the above comment with the following version of jdk1.8}}

7. Message Production interface

 Public Interface Inotifyservice {    /**     *  Send message      @author  jianzhang11     * @ Date 2018/4/13 16:52     @param  message     */    void  Sendnotify (String message);}

8. Inject Inotifyservice and call the Sendnotify method where it needs to be called

   @GetMapping ("Test")    @ResponseBody    public  String testlog () {        log.info ("= = =========");        Notifyservice.sendnotify ("hello,world!" );         return "Hello,world";    }

Spring Integrated Disruptor3

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.