[Openfire plug-in Development] iqhandler processes IQ request packets (template method mode)

Source: Internet
Author: User

Original link: http://www.hechunchen.info /? P = 15

 

We know that openfire plug-in development mainly involves three registration methods: 1) iqhandler (IQ handlers respond to IQ packets with a participant 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 participant sub-domain ).

Today, iqhandler is involved in a design pattern-the template method pattern.

The most iconic feature of the template method is that it requires an abstract class. In Java design patterns, abstract classes are rarely used, and interfaces are more used.

If we want to process our IQ package, we can customize classes such as testtemplatemethodhandler: Class testtemplatemethodhandler extends iqhandler, and then in public iqhandlerinfo getinfo () enter the name and namespace of the element you want to register in the method. In public IQ handleriq (IQ packet) in the method, write the information about the IQ package you want to perform (note that the IQ package is based on the Q & A format, so reply of the IQ package should be included ).

Sample Code:

 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 }

 

Note that other methods of the parent class must call the abstract method. Otherwise, the subclass Writing Method (small details of the algorithm) is not used at all. For example, the handleriq method is called in void process (packet) in iqhandler.

To sum up, openfire uses the template method mode very well. The template method mode is very suitable for systems that may perform secondary development. We have defined a large Algorithm Framework in the parent class, and can postpone the determination of some small details to the subclass for implementation. Just like iqhandler has already written a big Algorithm Framework. After the second developer just needs extends iqhandler, he writes two methods to implement it and process the missing IQ package. We don't have to worry about how IQ packets are routed to us. Obviously, the template method reduces the difficulty of secondary development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.