首页 > 代码库 > OpenFire源码学习之二十:在openfire中使用redis插件(下)
OpenFire源码学习之二十:在openfire中使用redis插件(下)
Redis插件实现
首先来看下插件目录:
RedisServicePlugin
源码清单:
import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.container.Plugin; import org.jivesoftware.openfire.container.PluginManager; import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.PropertyEventDispatcher; import org.jivesoftware.util.PropertyEventListener; import org.jivesoftware.util.redis.enity.RedisConfig; import org.jivesoftware.util.redis.enity.User; import org.jivesoftware.util.redis.enity.UserVcard; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; public class RedisServicePlugin implements Plugin, PropertyEventListener{ private static final Logger log = LoggerFactory.getLogger(RedisServicePlugin.class); private static final String OF_REDIS_CONFIG_DELETE = "delete from redisConfiguration"; private static final String OF_REDIS_CONFIG_INSERT = "INSERT INTO redisConfiguration (name, ip, port, auto) VALUES (?, ?, ?, ?)"; private XMPPServer server; private boolean enabled; @Override public void propertySet(String property, Map<String, Object> params) { if (property.equals("plugin.redis.enabled")) { this.enabled = Boolean.parseBoolean((String)params.get("value")); } } ...... public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; JiveGlobals.setProperty("plugin.redis.enabled", enabled ? "true" : "false"); } @Override public void initializePlugin(PluginManager manager, File pluginDirectory) { enabled = JiveGlobals.getBooleanProperty("plugin.redis.enabled", true); server = XMPPServer.getInstance(); PropertyEventDispatcher.addListener(this); } @Override public void destroyPlugin() { enabled = JiveGlobals.getBooleanProperty("plugin.redis.enabled", false); PropertyEventDispatcher.removeListener(this); } public void initializeAllData () { Collection<RedisConfig> crcfs = getAllRedisConfig(); if (!crcfs.isEmpty()) { for (RedisConfig rcf : crcfs) { if ("REDIS_USER".equals(rcf.getName())) { setRedisByUserInfo(); } } } } public void saveRedisConfig(Collection<RedisConfig> object) { PreparedStatement pstmt = null; Connection con = null; try { con = (Connection) DbConnectionManager.getConnection(); if (object != null) { pstmt = con.prepareStatement(OF_REDIS_CONFIG_DELETE); pstmt.execute(); pstmt = (PreparedStatement) con.prepareStatement(OF_REDIS_CONFIG_INSERT); for (RedisConfig cr : object) { pstmt.setString(1, cr.getName()); pstmt.setString(2, cr.getIp()); pstmt.setString(3, cr.getPort()); pstmt.setString(4, cr.getAuto()); pstmt.executeUpdate(); } } } catch (Exception e) { log.info(e.getMessage()); e.printStackTrace(); } finally { DbConnectionManager.closeConnection(pstmt, con); } } public Collection<RedisConfig> getAllRedisConfig() { if (server == null) server = XMPPServer.getInstance(); return server.getJedisConfDao().getAllRedisConfig(); } public void setRedisOfflineMessage() { XMPPServer.getInstance().getChatMessageJedisPoolManager().initDataByOfflineMessage(); } public void destroyOfflineMessage() { Jedis jedis = server.getChatMessageJedisPoolManager().getJedis(); jedis.flushDB(); server.getChatMessageJedisPoolManager().returnRes(jedis); } public void setRedisUserSearch() { XMPPServer.getInstance().getRedisSearchManager().createSearchAllUserData(); } public void destroyUserSearch() { Jedis jedis = server.getRedisSearchManager().getSearchJedis(); jedis.flushDB(); XMPPServer.getInstance().getUserJedis().returnRes(jedis); } public void destroyRedisByUserInfo () { Jedis jedis = server.getUserJedis().getJedis(); Map<String, String > user = jedis.hgetAll("OFUSER:admin"); jedis.flushAll(); jedis.hmset("OFUSER:admin", user); XMPPServer.getInstance().getUserJedis().returnRes(jedis); } public void setRedisByUserInfo () { Jedis jedis = server.getUserJedis().getJedis(); int i = 1; Collection<User> users = server.getUserJedis().getAllUser(); if (!users.isEmpty()) { for (User user : users) { Map<String,String> map = new HashMap<String, String>(); map.put("PASSWORD", user.getPassword()); if (user.getName() != null ) map.put("NAME", user.getName()); if (user.getMoblie() != null ) map.put("MOBILE", user.getMoblie().trim()); if (user.getEmail() != null) map.put("EMAIL", user.getEmail().trim()); map.put("CREATIONDATE",user.getCreationDate().trim()); map.put("MODIFICATIONDATE",user.getModificationDate().trim()); jedis.hmset("OFUSER:" + user.getUsername(), map); System.out.println("The " + i++ + "user generation=> username:" + user.getUsername() + ", name:" + user.getName()); } System.out.println("the end:user base info generated..."); i = 1; for (User user : users) { if (user.getMoblie() != null && !"".equals(user.getMoblie())) { jedis.set("MOBILE:" + user.getMoblie(), user.getUsername()); System.out.println("The" + i++ + "user,mobile relation generation=> mobile:" + user.getMoblie() + ", username:" + user.getUsername()); } } System.out.println("the end:user and mobile relation generated..."); i = 1; } Collection<UserVcard> uservcards = server.getUserJedis().getUserVcard(); if (!uservcards.isEmpty()) { for (UserVcard uv : uservcards) { if (uv.getVcard() != null && !"".equals(uv.getVcard())) { jedis.set("OFVCARD:" + uv.getUsername(), uv.getVcard()); System.out.println("The" + i++ + "user vcard generation=> username:" + uv.getUsername()); } } i = 1; System.out.println("the end:user vcard generated..."); } server.getUserJedis().returnRes(jedis); } }
RedisUserInfoOpt:
import java.util.HashMap; import java.util.Map; import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.util.redis.enity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; public class RedisUserInfoOpt { private static final Logger Log = LoggerFactory.getLogger(RedisUserInfoOpt.class); public void setBaseUserInfo(User user) { if (user != null) { Jedis jedis = XMPPServer.getInstance().getUserJedis().getJedis(); try { Map<String,String> map = new HashMap<String, String>(); map.put("PASSWORD", user.getPassword()); if (user.getName() != null ) map.put("NAME", user.getName()); if (user.getMoblie() != null ) map.put("MOBILE", user.getMoblie().trim()); if (user.getEmail() != null) map.put("EMAIL", user.getEmail().trim()); map.put("CREATIONDATE",user.getCreationDate().trim()); map.put("MODIFICATIONDATE",user.getModificationDate().trim()); jedis.hmset("OFUSER:" + user.getUsername(), map); Log.info("user generation=> username:" + user.getUsername() + ", name:" + user.getName()); } finally{ XMPPServer.getInstance().getUserJedis().returnRes(jedis); } } } public void setUserMobile(User user) { if (user != null) { Jedis jedis = XMPPServer.getInstance().getUserJedis().getJedis(); try { } finally { XMPPServer.getInstance().getUserJedis().returnRes(jedis); } } } }
redis-service.jsp
<%@ page import="java.util.*, org.jivesoftware.openfire.XMPPServer, org.jivesoftware.util.*, com.montnets.redis.plugin.RedisServicePlugin, org.jivesoftware.util.redis.enity.RedisConfig" errorPage="error.jsp" %> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %> <%-- Define Administration Bean --%> <jsp:useBean id="admin" class="org.jivesoftware.util.WebManager" /> <c:set var="admin" value=http://www.mamicode.com/"${admin.manager}" />>Plugin.xml
<?xml version="1.0" encoding="UTF-8"?> <plugin> <class>com.montnets.redis.plugin.RedisServicePlugin</class> <name>Redis Service</name> <description>Redis distributed cache</description> <author>HuWenFeng</author> <version>1.3.3</version> <date>17/1/2014</date> <minServerVersion>3.8.2</minServerVersion> <adminconsole> <tab id="tab-redis" name="Redis" url="redis-service.jsp"> <sidebar id="sidebar-redis-settings" name="Redis Service" > <item id="redis-service" name="Redis Service" url="redis-service.jsp" description="Redis distributed cache" /> </sidebar> </tab> </adminconsole> </plugin>这里只简单贴出了用户这类的相关数据的预加载。
下面开始具体讲讲基于Redis数据的几大分类。这里这里主要拿用户和消息这两大块具体来说说openfire基于redis的优化。
OpenFire源码学习之二十:在openfire中使用redis插件(下)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。