消息注册机制更加丰富了MessageHandle的流程,它把过程严格阶段化了,分成消息注册和通知消息处理两个部分,让人一目了然。
消息注册机制的总体思想是:一个对象中开辟一个空间用于存放Message,当调用regist方法时将Message存放进去,当其调用notify方法时将所有Message取出并发送到MessageQueue中等待处理。
下面是模拟程序,提供Registrant,RegistrantList,AsyncResult,MiddleUser类
01
package com.zte.liu.registermessage;
02
03
public class AsyncResult {
04
05
MessageParmeters parmList;
06
Object result;
07
Throwable exception;
08
09
public Throwable getException() {
10
return exception;
11
}
12
13
public void setException(Throwable exception) {
14
this.exception = exception;
15
}
16
17
AsyncResult(Object result, Throwable exception){
18
parmList = new MessageParmeters();
19
this.result = result;
20
this.exception = exception;
21
}
22
23
public MessageParmeters getMessageParms(){
24
return parmList;
25
}
26
27
public Object getResult(){
28
return result;
29
}
30
31
public void setResult(Object result){
32
this.result = result;
33
}
34
35
public AsyncResult refresh(Object parm){
36
parmList.refresh(parm);
37
return this;
38
}
39
}
01
package com.zte.liu.registermessage;
02
03
import com.zte.liu.messagehandler.Handler;
04
05
public class MiddleUser {
06
07
RegistrantList registrantList = new RegistrantList();
08
09
public void registForReason(Handler handler, int what, Object firstParms){
10
registrantList.add(handler, what, firstParms);
11
}
12
13
public void notifyRegistrant(Object secondParms, Throwable exception){
14
registrantList.notifyRegistrantList(secondParms, exception);
15
}
16
17
public void unRegistForReason(int what){
18
registrantList.remove(what);
19
}
20
}
01
package com.zte.liu.registermessage;
02
03
import com.zte.liu.messagehandler.Handler;
04
import com.zte.liu.messagehandler.Message;
05
06
public class Registrant {
07
08
private Message msg = null;
09
10
Registrant(Handler handler, int what, Object firstParms){
11
msg = new Message(what, handler, new AsyncResult(null,null).refresh(firstParms));
12
}
13
14
public void notifyRegistrant(Object secondParms, Throwable exception){
15
if(msg == null){
16
return;
17
}
18
AsyncResult asyncResult = (AsyncResult)msg.getObj();
19
asyncResult.refresh(secondParms).setException(exception);
20
msg.sendToTarget();
21
}
22
23
public Message getMessage(){
24
return msg;
25
}
26
}
01
package com.zte.liu.registermessage;
02
03
import java.util.ArrayList;
04
05
import com.zte.liu.messagehandler.Handler;
06
07
public class RegistrantList {
08
09
private ArrayList<Registrant> registrantList = new ArrayList<Registrant>();
10
11
public synchronized void add(Handler handler, int what, Object firstParms){
12
Registrant registrant = new Registrant(handler, what, firstParms);
13
registrantList.add(registrant);
14
}
15
16
public synchronized void remove(int what){
17
for(int i=0; i<registrantList.size(); i++){
18
if(((Registrant)registrantList.get(i)).getMessage().getId() == what){
19
registrantList.remove(i);
20
}
21
}
22
}
23
24
public synchronized void notifyRegistrantList(Object secondParms, Throwable exception){
25
for(int i=0; i<registrantList.size(); i++){
26
Registrant registrant = (Registrant)registrantList.get(i);
27
registrant.notifyRegistrant(secondParms, exception);
28
}
29
}
30
31
}