MINA(1)Chain and Observer Basic Theory
MINA(1)Chain and Observer Basic Theory
Before, I just demo the MINA and use spring to integrate the MINA in my web application.
I learn MINA from others' blog, start from the basic design ideas.
1. Chain of filters, or rather say, handlers
One simple example for chain
Object iphone
package com.sillycat.easynio.plugins.mina.chain;
public class Iphone {
private String state = "IOS mobile phone!";
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Interface for Workers
package com.sillycat.easynio.plugins.mina.chain;
public interface IWorker {
public void handle(Iphone iphone);
public void setNextWorker(IWorker worker);
}
One implementation of workers based on the interface
package com.sillycat.easynio.plugins.mina.chain;
public class WorkerDevice implements IWorker {
private IWorker next;
public void handle(Iphone iphone) {
iphone.setState(iphone.getState() + " Hardware Device is OK - ");
if (next != null) {
next.handle(iphone);
}
}
public void setNextWorker(IWorker worker) {
this.next = worker;
}
}
The factory will arrange all the workers to work together:
package com.sillycat.easynio.plugins.mina.chain;
public class IphoneFactory {
public static void main(String[] args) {
IWorker worker1 = new WorkerDevice();
IWorker worker2 = new WorkerSoftware();
IWorker worker3 = new WorkerIntegration();
worker1.setNextWorker(worker2);
worker2.setNextWorker(worker3);
Iphone iphone = new Iphone();
worker1.handle(iphone);
System.out.println(iphone.getState());
}
}
How is chain used in MINA
IoFilterAdapter -----> IoFilter ---- NextFilter
Entry
EntryImpl
DefaultIoFiterChain
2. Watch and Observe
Observer and Subject, once the subject is changed, it will send event to observers.
class --------> java.util.Observer
interface---> java.util.Observable
Oberver means that the class is a observer. Observable means that the class is a subject. Other observers can observe it.
There is an exmaple for Heater, Display and Alarm.
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
public class Heater extends Observable {
private int temperature;
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public void boilWater() {
for (int i = 90; i < 100; i++) {
temperature = i;
this.setChanged();
this.notifyObservers(i);
}
}
}
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
import java.util.Observer;
public class Display extends Observable implements Observer {
private String status = "Not yet!";
public void setStatus(String status) {
this.status = status;
}
public void displayTemputer(int temperature) {
System.out.println("Status: " + status + " Temprature: " + temperature
+ "");
if (temperature > 95) {
this.setStatus("Boiled!");
this.setChanged();
this.notifyObservers();
}
}
public void update(Observable o, Object arg) {
System.out.print( "num is i = " + arg + " ");
displayTemputer(((Heater) o).getTemperature());
}
}
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
import java.util.Observer;
public class Alarm implements Observer {
public void makeAlarm() {
System.out.println("Alarm, alarm, the water is boiled! ");
}
public void update(Observable o, Object arg) {
makeAlarm();
}
}
package com.sillycat.easynio.plugins.mina.observer;
public class AlarmTest {
public static void main(String[] args) {
Heater header = new Heater();
Display display = new Display();
Alarm alarm = new Alarm();
header.addObserver(display);
display.addObserver(alarm);
header.boilWater();
}
}
Here is another interesting example about sexy girl, color wolf and sage.
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
public class SexyGirl extends Observable {
private String state = "With All Clothes!";
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void dance() {
this.state = " Without all Clothes!";
this.setChanged();
this.notifyObservers();
}
}
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
import java.util.Observer;
public class ColorWolf implements Observer {
public void update(Observable o, Object arg) {
SexyGirl subject = (SexyGirl)o;
if(subject != null){
System.out.println("Oh, I am color wolf, I see.");
}
}
}
package com.sillycat.easynio.plugins.mina.observer;
import java.util.Observable;
import java.util.Observer;
public class Sage implements Observer {
public void update(Observable o, Object arg) {
SexyGirl subject = (SexyGirl) o;
if (subject != null) {
System.out.println("I am sage, sexy is empty to me!");
}
}
}
package com.sillycat.easynio.plugins.mina.observer;
public class WolfTest {
public static void main(String[] args) {
SexyGirl girl = new SexyGirl();
ColorWolf wolf = new ColorWolf();
Sage sage = new Sage();
girl.addObserver(wolf);
girl.addObserver(sage);
girl.dance();
}
}
From observer to Event
In JDK,
java.util.EventListener
java.util.EventObject
In MINA,
IoServiceListener watch the service and session creating, idle and destroying.
IoFutureListener watch the IoFuture.
references:
http://sillycat.iteye.com/blog/563764
http://sillycat.iteye.com/blog/563765
http://www.iteye.com/topic/1124504
http://www.iteye.com/topic/1124605
http://www.iteye.com/topic/1124736
http://www.iteye.com/topic/1125178
http://kantery.iteye.com/blog/338715