人力资源openfire插件开发入门
openfire源码构建指导(E文):
html
android openfire 插件开发:
代码
实现功能,客户端发送一个IQ包含内容的节给服务器,服务器取得内容,然后广播给所有的在线用户
新定义的iq为
输入发言内容
服务器端:
建立org.jivesoftware.openfire.handler.IQHandler的实现类 view plaincopy to clipboardprint?
1. package test.plugin;
2.
3. import org.dom4j.Element;
4. import org.jivesoftware.openfire.IQHandlerInfo;
5. import org.jivesoftware.openfire.XMPPServer;
6. import org.jivesoftware.openfire.auth.UnauthorizedException;
7. import org.jivesoftware.openfire.handler.IQHandler;
8. import org.xmpp.packet.IQ;
9.
10. public class BroadcastsIQ extends IQHandler {
11.
12. private IQHandlerInfo info;
13. public BroadcastsIQ() {
14. super("用户广播模块");
15. info = new IQHandlerInfo("b", "com:message:broadcasts");
16. }
17.
18. @Override
19. public IQHandlerInfo getInfo() { 20. return info;
21. }
22.
23. @Override
24. public IQ handleIQ(IQ packet) throws UnauthorizedException {
25. Element iq = packet.getElement(); 26. Element b = iq.element("b"); 27. String text = b.getText(); 28. XMPPServer.getInstance().getSessionManager().sendServerMessage(nu
ll, text);//广播信息
29. return null;
30. }
31.
32. }
package test.plugin; import org.dom4j.Element; import org.jivesoftware.openfirimport org.jivesoftware.openfirimport org.jivesoftware.openfir
建立org.jivesoftware.openfire.container.Plugin的实现类
view plaincopy to clipboardprint?
1. package test.plugin;
2.
3. import java.io.File;
4. import java.util.List;
5.
6. import org.jivesoftware.openfire.XMPPServer;
7. import org.jivesoftware.openfire.container.Plugin;
8. import org.jivesoftware.openfire.container.PluginManager;
9. import org.jivesoftware.openfire.handler.IQHandler;
10.
11. public class MyPlugin implements Plugin {
12.
13. private IQHandler iQHandler;
14. @Override
15. public void destroyPlugin() {
16. XMPPServer.getInstance().getIQRouter().removeHandler(iQHandler);
17. System.out.println("插件停止成功");
18. }
19.
20. @Override
21. public void initializePlugin(PluginManager manager, File pluginDirect
ory) {
22. iQHandler = new BroadcastsIQ();
23. XMPPServer.getInstance().getIQRouter().addHandler(iQHandler);
24. System.out.println("插件运行成功");
25. }
26. }
package test.plugin; import java.io.File; import java.util.List; import org.jivesoftware.openfir
建立plugin.xml文件
view plaincopy to clipboardprint?
1.
2.
3. test.plugin.MyPlugin
4. Broadcasts messages
5. This is an Broadcasts messages plugin.
6. me
7.
8. 1.0
9. 01/01/2011/date>
10. none
11. 3.0.0
12. gpl
13.
14.
15.
test.plugin.MyPlugin Broadcasts messages This is an Bro me
打包为任意名称的jar文件
结构如下
test.jar
--classes
--test
--plugin
--BroadcastsIQ.class
--MyPlugin.class
--META-INF
--MANIFEST.MF
--plugin.xml
然后把打好包的jar文件放到openfire的plugins目录下,openfire会自动加载,观察控制
台是否输出 插件运行成功
客户端:
建立org.jivesoftware.smack.packet.IQ的实现类
view plaincopy to clipboardprint?
1. package test.xmpp;
2.
3. import org.jivesoftware.smack.packet.IQ;
4.
5. public class Broadcasts extends IQ {
6.
7. private String body;
8. public String getElementName() {
9. return "b";
10. }
11.
12. public String getNamespace() {
13. return "com:message:broadcasts";
14. }
15.
16. public void setBody(String body) {
17. this.body = body;
18. }
19.
20. public String getBody() {
21. return body;
22. }
23.
24. @Override
25. public String getChildElementXML() {
26. if(getBody() == null){
27. throw new RuntimeException("Broadcasts body is empty");
28. }
29. StringBuilder sb = new StringBuilder();
30. sb.append("<").append(getElementName()).append(" xmlns=\"").appen
d(getNamespace()).append("\">").append(getBody()).append("").append(get
ElementName()).append(">");
31. return sb.toString();
32. }
33.
34. }
public String getBody() { return body; } @Override
然后在程序中
view plaincopy to clipboardprint?
1. Broadcasts b = new Broadcasts();
2. b.setBody("测试广播内容");
3. b.setType(IQ.Type.SET);
4. conn.sendPacket(b);
Broadcasts b = new Broadcasts()b.setBody("测试广播内容"); b.setType(IQ.Type.SET); conn.sendPacket(b);
实现功能比较简单,算是学习openfire 插件开发的开始吧