OpenFire components (Component) Development

Source: Internet
Author: User

In the previous article "OpenFire Stage Practice Summary" mentioned a kind of openfire extension mode compoent. This paper will mainly discuss the application and development method of this model.

Introduction to internal and external components

Many of the plugins in OpenFire implement compoent,compoent flexibility in that it can be handled by a specific level two subdomain package. The most obvious usage scenario in the XMPP protocol is group chat, which is a typical example. See OpenFire in the chat room Jid is what format: [email protected], it is clear that there is a conference. Compare users ' jid:[email protected]. OpenFire provides routing capabilities for this seed domain through a registered router.

This mechanism provides a flexible extension scenario in which you can fully define your own protocol processing so that OpenFire is present as a message relay center. More complex businesses can be implemented within their own components.

Of course, to enrich the expansion, OpenFire provides both internal and external components

    • Internal components, mainly in the form of plug-ins, jar packages. Internal components can have the same access and control rights as the primary domain. For example you want to get all the users in the primary domain that's okay.
    • External components, but a standalone application that connects to OpenFire in TCP, of course, cannot acquire resources in the primary domain.

The two components of the application of different scenarios, the internal components can be compared with the main domain implementation of the close, basically is openfire part, such as you want to expand the group chat as QQ form of the group, you can use the internal components to implement. And if the business system integration needs to integrate some functions of openfire, you can choose the external component mode, so it is more convenient. For example, your mall needs an online client robot, so you can choose external components.

The main development package

Two development kits, Tinder and whack are available in OpenFire.

    • Tinder

The main package is the basic package of the XMPP protocol, developed by Java. This package is referenced in OpenFire, so basically the server uses this protocol package.

    • Whack

A development package developed by external components is provided on a tinder basis, making it easier for developers to build openfire external components.

This shows that Tinder is a core, which is also better for various projects, including OpenFire itself. And whack is more like a toolkit for quick development of external components that are easily integrated into Java projects. Tinder and whack are maven packages, so it's much easier for MAVEN projects. Unlike OpenFire, which is ant, it is not very familiar at first.

implement a simple robot

Then implement a simple auto-reply robot to show how the component is developed.

1, the creation of a robot, the robot is mainly to achieve the function of automatic recovery, so the robot is more stupid, will only say three words, and only a random reply. The code is as follows:

 PackageOrg.jivesoftware.demo;ImportJava.util.Random;ImportOrg.xmpp.packet.Message; Public classRobotservice {Private Static FinalRobotservice INSTANCES =NewRobotservice (); PrivateString[] autoreply = {"Hello I am robot Big G, glad to chat with you", "Oh, what do you say?" "Come again next time, it's a little busy today." "}; PrivateRobotservice () {}PrivateString getautoreply () {random random=NewRandom (); Integer idx=Random.nextint (autoreply.length); returnAutoreply[idx]; }         Public synchronized StaticRobotservice getinstance () {returnINSTANCES; }         Publicmessage Reply (message msg) {message Reply=NewMessage ();        Reply.setid (Msg.getid ());        Reply.setto (Msg.getfrom ());        Reply.setfrom (Msg.getto ());        Reply.settype (Message.Type.chat);        Reply.setbody (Getautoreply ()); returnreply; }}

Robots will automatically find a reply from the language they have learnt.

2. Implement external components

Because robot Auto-reply does not require too much interaction with openfire, it is only necessary to make an external component. Transfer the message from the front to the specific robot component for processing. What is needed here is to implement the Abstractcomponent abstract class.

 PackageOrg.jivesoftware.demo;Importorg.xmpp.component.AbstractComponent;ImportOrg.xmpp.packet.Message; Public classRobotcomponentextendsabstractcomponent{PrivateString name; PrivateString Serverdomain;  Publicrobotcomponent (string name, String serverdomain) { This. Name =name;  This. Serverdomain =Serverdomain; } @Override PublicString getdescription () {return"I'm a robot."; } @Override PublicString GetName () {returnname; } @Overrideprotected voidhandlemessage (Message message) {if((message.getbody () = =NULL)) {            return; }        //use robot to replyMessage reply =robotservice.getinstance ().        Reply (message);    Send (reply); }}

This is a abstractcomponent abstract class, which is provided in Tinder to simplify the development of component. In fact, the IQ, mesage, disco and other package processing has been encapsulated and provided an overriding method to the derived class implementation. Developers only need to care about the specific implementation, do not care about the resolution and processing of the protocol. And if the direct implementation of the component interface will be to parse the Protocol namespace, and then the specific processing.

Since the robot is simply an automatic response in this application, only the Handlemessage method can be implemented. This method automatically gets the message packets sent over. And we just need to send the message back to the sender.

3. Register the external components with the OpenFire

This is relatively simple, look directly at the code:

 PackageOrg.jivesoftware.demo;Importorg.jivesoftware.weather.WeatherComponent;ImportOrg.jivesoftware.whack.ExternalComponentManager;Importorg.xmpp.component.ComponentException; Public classRobotdemoserver { Public Static voidMain (string[] args) {FinalExternalcomponentmanager Manager =NewExternalcomponentmanager ("localhost", 5275); Manager.setsecretkey ("Robot", "Test"); Manager.setmultipleallowed ("Robot",true); Try{manager.addcomponent ("Robot",NewRobotcomponent ("Robot", Manager.getservername ())); //make the program do not quit             while(true) {                Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); }            }        } Catch(componentexception e) {e.printstacktrace (); }    }}

The main one is Externalcomponentmanager, a class that whack provides a wrapper class for connecting to OpenFire Component Services.

The ports in the server address and port refer to the external component access port, which can be set on the OpenFire server.

Setsecretkey is the password used to set the connection, which is also based on the settings of the server.

The settings for the server are as follows:

Then start to try it and send a message to the robot.

     Public Static void Testsendmessage () {                new Message ("[Email protected]" + connection.getservicename ());            Msg.setbody ("Hello Robot");         Try {            Connection.sendstanza (msg);         Catch (notconnectedexception e) {            System.err.println ("send error." + E.getmessage ());        }    }

OpenFire components (Component) 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.