Event Listener Implementation:
Three elements:
1. Event source (data source, data to be processed)
2. Events (carrying data, transmitting information and being monitored)
3, the Listener (responsible for the data business processing)
--This development case uses spring's event monitoring
1. Defining Event Types
Public class MyEvent extends applicationevent {
Private Static Final Long Serialversionuid = 7937618461275424515L;
Other properties (not required)
... ...
Public MyEvent (Object source) {
Super (source);
}
}
2, define a basic listening abstract class (generally dealing with some common things, not necessary, recommend the use of abstract classes)
Public Abstract class Baseapplicationlistener implements Applicationlistener {
Private Static Logger Logger = Logger. GetLogger (Baseapplicationlistener. class);
This abstract method requires the implementation of the specific business processing of the class-to-hear event
Public Abstract String process (Applicationevent event);
Public Final void onapplicationevent (applicationevent event) {
String message = Process (event);
Record log
Logger.info (message);
Other processing
... ...
}
}
3, define the specific implementation class
Public class MyListener extends baseapplicationlistener {
@SuppressWarnings ("Unchecked")
Public String process (Applicationevent event) {//Implements specific business handling of the events heard
String message = "";
Handling Events
if (event!=null) {
Judging event Types
if (Event instanceof myevent) {
Some specific business processes
... ...
}
}
return message;
}
}
other Service you need to publish the event method code:
Data is the business data object to be processed
ContextUtil. Getapplicationcontext (). Publishevent (new myevent (data));
Tool class:
Public class ContextUtil implements Applicationcontextaware {
Private ContextUtil () {
Nothing
}
Private Static ApplicationContext ApplicationContext; Spring Application Context Environment
/**
* Implement the callback method of the Applicationcontextaware interface, set the context environment
*
* @param ApplicationContext
* @throws beansexception
*/
Public void setapplicationcontext (ApplicationContext applicationcontext) throws beansexception {
ContextUtil. ApplicationContext = ApplicationContext;
}
/**
* @return ApplicationContext
*/
Public Static ApplicationContext Getapplicationcontext () {
return ApplicationContext;
}
/**
* Get Objects
*
* @param Name
* @return Object An instance of a bean registered with the given name
* @throws beansexception
*/
Public Static Object Getbean (String name) throws beansexception {
return applicationcontext. Getbean (name);
}
/**
* Get an object of type Requiredtype
* If the bean cannot be converted by type, the corresponding exception will be thrown (beannotofrequiredtypeexception)
*
* @param Name
* Bean Registry Name
* @param requiredtype
* Return Object type
* @return object returns requiredtype type objects
* @throws beansexception
*/
Public Static Object Getbean (String name, Class requiredtype) throws beansexception {
return applicationcontext. Getbean (name, requiredtype);
}
/**
* Returns True if Beanfactory contains a bean definition that matches the given name
*
* @param Name
* @return Boolean
*/
Public Static boolean Containsbean (String name) {
return applicationcontext. Containsbean (name);
}
/**
* Determine whether the bean defined as registered in the given name is a singleton or a prototype.
* If the bean definition corresponding to the given name is not found, an exception will be thrown (nosuchbeandefinitionexception)
*
* @param Name
* @return Boolean
* @throws nosuchbeandefinitionexception
*/
Public Static boolean Issingleton (String name) throws nosuchbeandefinitionexception {
return applicationcontext. Issingleton (name);
}
/**
* @param Name
* Type of @return Class registered Object
* @throws nosuchbeandefinitionexception
*/
Public Static Class GetType (String name) throws nosuchbeandefinitionexception {
return applicationcontext. GetType (name);
}
/**
* If the given bean name has an alias in the bean definition, these aliases are returned
*
* @param Name
* @return
* @throws nosuchbeandefinitionexception
*/
Public Static String[] Getaliases (String name) throws nosuchbeandefinitionexception {
return applicationcontext. getaliases (name);
}
}
Java Event Monitoring