Yran programming has many areas where the event-driven model is used, simplifying the code and making it easier to understand.
1. Define an event:
Import org.apache.hadoop.yarn.event.AbstractEvent;
public class Taskevent extends abstractevent<taskeventtype> {
private String TaskID;
Public Taskevent (String TaskID, Taskeventtype type) {
super (type);
This.taskid=taskid;
}
Public String GetTaskID () {
return taskID;
}
}
2. There are 2 types of this event:
public enum Taskeventtype {
t_kill,
t_schedule
}
3. Define a scheduler, and the test code is also written in:
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.service.CompositeService;
Import Org.apache.hadoop.service.Service;
Import org.apache.hadoop.yarn.conf.YarnConfiguration;
Import Org.apache.hadoop.yarn.event.AsyncDispatcher;
Import Org.apache.hadoop.yarn.event.Dispatcher;
Import Org.apache.hadoop.yarn.event.EventHandler;
public class Simplemrappmaster extends Compositeservice {//Single Event Service can inherit Abstractservice, how to use it.
Private Dispatcher Dispatcher;
Public Dispatcher Getdispatcher () {return Dispatcher;
} public Simplemrappmaster (String name) {super (name);
} @Override protected void Serviceinit (Configuration arg0) throws Exception {dispatcher = new asyncdispatcher ();
Dispatcher.register (Taskeventtype.class, New Taskeventdisptacher ());
AddService (Service) dispatcher);
Super.serviceinit (arg0); } Private class Taskeventdisptacher implements eventhandler<taskevent>{@Override public void handle (Taskeve NT arg0) {if (arg0.getType () = = Taskeventtype.t_kill) {System.out.println ("Run KILL");
}else if (arg0.gettype () = = Taskeventtype.t_schedule) {System.out.println ("Run SCHEDULE"); }}}//test portal public static void main (string[] args) throws Exception {Simplemrappmaster master = new Simpl
Emrappmaster ("Simplemrappmaster");
Yarnconfiguration conf = new yarnconfiguration ();
Master.serviceinit (conf);
Master.servicestart ();//Must be a subclass to be called, so the Main method writes to this class, and the main method writes to other classes, which need to be reflected. Master.init (conf); These 2 groups of methods test the effect looks the same,//Master.start ();
Have time to see the API explained Master.getdispatcher (). Geteventhandler (). Handle (New Taskevent ("Task1", Taskeventtype.t_schedule));
Master.getdispatcher (). Geteventhandler (). Handle (New Taskevent ("Task1", Taskeventtype.t_kill)); }
}
There are some problems, do not understand, first make a record it.