【openfire插件開發】IQHandler處理IQ請求包(模板方法模式)


原文鏈接:http://www.hechunchen.info/?p=15

 

        我們知道openfire插件開發主要有3種方式注冊方式:1)IQHandler(IQ handlers respond to IQ packets with a particular element name and namespace),2)Interceptor(PacketInterceptor to receive all packets being send through the system and optionally reject them),3)Component(Components receive all packets addressed to a particular sub-domain)。

        今天來講IQHandler涉及到的一種設計模式——模板方法(Template Method)模式。

        模板方法最標志性的特點,是,他需要一個抽象類。在java設計模式中,其實很少用到抽象類,更多地還是用到接口。

        我們如果要做自己的IQ包處理,可以自定義類如TestTemplateMethodHandler:class TestTemplateMethodHandler extends IQHandler,然后在public IQHandlerInfo getInfo()方法中寫上自己想要注冊的元素名及命名空間,在public IQ handlerIQ(IQ packet)方法中寫上自己想要對丟進來的IQ包做什么樣的處理(注意IQ包是基於問答形式的,所以應該有IQ包的reply)。

        給一段示例代碼:

 1 import org.jivesoftware.openfire.IQHandlerInfo;
2 import org.jivesoftware.openfire.auth.UnauthorizedException;
3 import org.jivesoftware.openfire.handler.IQHandler;
4 import org.xmpp.packet.IQ;
5
6 public class TestTemplateMethodHandler extends IQHandler
7 {
8 private IQHandlerInfo info;
9
10 public TestTemplateMethodHandler()
11 {
12 super("TestTemplateMethodHandler");
13 info = new IQHandlerInfo("query", "http://hechunchen.info/querytest");
14 }
15 @Override
16 public IQHandlerInfo getInfo()
17 {
18 // TODO Auto-generated method stub
19 return info;
20 }
21
22 @Override
23 public IQ handleIQ(IQ packet) throws UnauthorizedException
24 {
25 // TODO Auto-generated method stub
26 IQ reply = IQ.createResultIQ(packet);
27 System.out.println("iq from:" + reply.getFrom());
28 System.out.println("iq to:" + reply.getTo());
29 return reply;
30 }
31 }

 

        需要注意的是:父類的其他方法是一定會調用它abstract的方法。不然,子類寫的方法(算法的小細節)根本沒有被用到。比如handlerIQ方法就在IQHandler中的void process(Packet packet)調用到。

        總結來講,openfire用Template Method這個模式非常到位。模板方法模式非常適合於可能會做二次開發的系統。我們在父類中定義好了算法的大框架,將一些小細節的確定可以推遲到子類中去實現。就像IQHandler已經寫好了算法大框架,我們二次開發者只需要extends IQHandler之后,自己寫上2個方法的實現,處理丟過來的IQ包。我們完全不用管IQ包是怎么路由到我們這里來的。顯然,Template Method降低了二次開發的難度。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM