Jstorm and Storm Source Analysis (v)--spoutoutputcollector and agent mode

Source: Internet
Author: User
Tags emit

This article is mainly to parse Spoutoutputcollector source code, by the way to analyze the design patterns involved in the class-proxy mode.
First introduce the spout output collector interface –ispoutoutputcollector, which mainly declares the following 3 abstract methods used to constrain the implementation class Ispoutoutputcollector. The interface definition and method descriptions are as follows:

/** * ispoutoutputcollector:spout Output Collector Interface */public interface Ispoutoutputcollector {    /**     * Modify method to send data outward, Its return value is the TaskID collection of all the sending targets of the message;     * Parameters:     * streamid: Message tuple will be output to the stream     * Tuple: The message to be output is an object list     * MessageId: The token information of the output message, if MESSAGEID is set to NULL, Storm will not track the message,     * Otherwise it will be used to track the message processing situation *    /list<integer> emit (String Streamid, list<object> Tuple, Object messageId);    /**     * This method is similar to the above emit method, except that:     * 1. The data (message) is only received by the task specified by the TaskID, (this means that if no downstream node receives the message, the message is not actually sent)     * 2. This method requires that the flow corresponding to the parameter streamid must be a direct stream, and the task on the receiving side must receive the message in a direct grouping,     * Otherwise an exception will be thrown.     *    /void emitdirect (int taskId, String streamid, list<object> tuple, Object messageId);    /**     * Used to handle exceptions *    /void ReportError (Throwable error);}

Storm provides the default class Spoutoutputcollector for interface Ispoutoutputcollector, a class that is actually a proxy class that holds an object of type Ispoutoutputcollector. All operations are actually implemented by the object. Spoutoutputcollector is defined as follows:

public class Spoutoutputcollector implements Ispoutoutputcollector {/** * holds Spoutoutputcollector object to proxy */I    Spoutoutputcollector _delegate;    Public Spoutoutputcollector (Ispoutoutputcollector delegate) {_delegate = delegate;  }/** * Implements the emit method in the interface and provides several overloaded methods of it * eg. if you do not specify Streamid, default is used, and if you do not specify MessageID, the default is null (NULL) */Public list<integer> emit (String streamid, list<object> tuple, Object messageId) {return _delegate.emit (Strea    MId, tuple, messageId); } public list<integer> emit (list<object> tuple, Object messageId) {return emit (Utils.default_stream    _id, tuple, messageId);    Public list<integer> emit (list<object> tuple) {return emit (tuple, null); } public list<integer> emit (String streamid, list<object> tuple) {return emit (streamid, tuple, null    );     }/** * Implements the Emitdirect method in the interface, and also provides several overloaded methods, consistent with the emit method above. */public void emitdirect (int taSkId, String Streamid, list<object> tuple, Object messageId) {_delegate.emitdirect (taskId, streamid, tuple,    MESSAGEID); } public void Emitdirect (int taskId, list<object> tuple, Object messageId) {emitdirect (taskId, Utils.defa    ult_stream_id, tuple, messageId); } public void Emitdirect (int taskId, String streamid, list<object> tuple) {emitdirect (taskId, Streamid, t    Uple, NULL);    } public void Emitdirect (int taskId, list<object> tuple) {emitdirect (taskId, tuple, null); }/** * Handles exception method implementation */@Override public void ReportError (Throwable error) {_delegate.reporterror (er    ROR); }}

Ps:
The proxy mode is divided into two main types: static agent and dynamic agent

Static Proxy:
Before the program runs, the relationship between the proxy class and the delegate class is determined before it runs, that is, the bytecode file for the proxy class already exists before the program runs.
Proxy mode role:
Subject (Abstract theme role): Can be an abstract class can also be an interface, declaring the delegated role and the delegate class common processing method;
Realsubject (Specific subject role): Also known as delegated role, the role of the agent, is the specific executor of business logic;
Proxysubject (Agent topic role): Also called the delegate class, the proxy class, is responsible for the real role application,
Delegate the method constraints defined by all abstract topic classes to specific topic roles, and do preprocessing and aftercare before and after the specific topic roles have been processed.

The static proxy mode case is as follows:

Abstract topic public interface Subject {public    void process (String taskname);

Role by proxy:

public class Realsubject implements Subject {    @Override public    void process (String taskname) {        System.out.println ("Performing tasks:" +taskname);        try {            thread.sleep;        } catch (Interruptedexception e) {            //TODO auto-generated catch block            E.printstacktrace ();}}}    

Proxy class:

public class Proxysubject implements Subject {    //proxy class holds a delegate class object reference    private Subject delegate;    Public Proxysubject (Subject delegate) {        this.delegate=delegate;    }    @Override public    void process (String taskname) {        //preprocessing        this.before ();        Assign the request to the delegate class processing        delegate.process (taskname);        Aftercare treatment        this.after ();    }    private void Before () {        System.out.println ("Preprocessing!");    }    private void After () {        System.out.println ("Aftercare!");}    

Case TEST:

public class Test {public    static void Main (string[] args) {        realsubject subject = new Realsubject ();        Proxysubject p = new Proxysubject (subject);        P.process ("draining");}    }

Test results:

Pre-treatment! Performing tasks: Drainage after treatment!

Advantages and disadvantages of static proxy classes:
Advantages:
The business class simply focuses on the business logic itself, thus guaranteeing the reusability of the business class.
Disadvantages:
An interface of a proxy object serves only one type of object. When there are many ways to broker, you need to proxy for each method. As a result, static agents do not work well when the program size becomes larger.

Dynamic Agent:
The relationship between the proxy class and the delegate class is determined when the program runs. The source code of the dynamic proxy class is generated dynamically by the JVM based on the reflection mechanism during the program run, so there is no bytecode file for the proxy class.

The dynamic proxy mode case is as follows:

Public interface Service {    //target method public     void process ();}
public class Userserviceimpl implements Service {    @Override public    void process () {         System.out.println (" User Service Processing ");}      }

Examples of dynamic proxy implementations:

public class Myinvocatiohandler implements Invocationhandler {    private Object target;    Public Myinvocatiohandler (Object target) {        this.target = target;    }    @Override Public    object Invoke (Object proxy, Method method, object[] args)            throws Throwable {        // SYSTEM.OUT.PRINTLN ("-----Before-----");        This.before ();        Object result = Method.invoke (target, args);       SYSTEM.OUT.PRINTLN ("-----End-----");        This.after ();        return result;    }    Build proxy Object public Object    GetProxy () {        ClassLoader loader = Thread.CurrentThread (). Getcontextclassloader ();        Class<?>[] interfaces = Target.getclass (). Getinterfaces ();        return proxy.newproxyinstance (loader, interfaces, this);    }    private void Before () {        System.out.println ("Preprocessing!");    }    private void After () {        System.out.println ("Aftercare!");}    

Case TEST:

public class Proxytest {public    static void Main (string[] args) {        Service service = new Userserviceimpl ();        Myinvocatiohandler handler = new Myinvocatiohandler (service);        Service serviceproxy = (service) handler.getproxy ();        Serviceproxy.process ();    }}

Test results:

Pre-processing! User Service handling Aftercare!

Advantages and disadvantages of dynamic agents:
Advantages:
All the methods in the interface are transferred to the calling processor in a centralized method in the method "runtime" dynamic join, determine what type you are, more flexible
Disadvantages:
1. Efficiency is reduced compared to static agents
2. The JDK dynamic agent can only proxy for classes that implement an interface

Please pay attention to the following QR code for technical exchanges:

Jstorm and Storm Source Analysis (v)--spoutoutputcollector and agent mode

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.