源码详解openfire保存消息记录_修改服务端方式

实现openfire消息记录通常有两种方式,修改服务端和添加消息记录插件。

今天,简单的说明一下修改服务端方式实现消息记录保存功能。


实现思路

修改前:

默认的,openfire只提供保存离线记录至ofOffline表中。当发送一条消息时,判断用户是否在线,若为true,不保存消息;若为fasle,保存消息至离线消息表中。


修改后:

仿照保存离线消息,用户每发送一条消息,将消息存放在ofHistory表中,ofHistory表结构同ofOffline


实现步骤:

1.修改初始数据库文件,路径src/database/openfire_sqlserver.sql

添加ofHistory表

  1. CREATE TABLE ofHistory (  
  2.   username              NVARCHAR(64)    NOT NULL,  
  3.   messageID             INTEGER         NOT NULL,  
  4.   creationDate          NVARCHAR(64)    NOT NULL,  
  5.   messageSize           INTEGER         NOT NULL,  
  6.   stanza                TEXT           NOT NULL,  
  7.   CONSTRAINT ofHistory_pk PRIMARY KEY (username, messageID)  
  8. );  
  9.   
  10. CREATE TABLE ofOffline (  
  11.   username              NVARCHAR(64)    NOT NULL,  
  12.   messageID             INTEGER         NOT NULL,  
  13.   creationDate          CHAR(15)        NOT NULL,  
  14.   messageSize           INTEGER         NOT NULL,  
  15.   stanza                NTEXT           NOT NULL,  
  16.   CONSTRAINT ofOffline_pk PRIMARY KEY (username, messageID)  
  17. );  

注:其他数据库修改方式雷同

2.添加保存消息方法

MessageRouter类中110行

[java]  view plain copy print ?
  1. try {  
  2.                  // Deliver stanza to requested route   
  3.                  routingTable.routePacket(recipientJID, packet, false);  
  4.                  //保存消息记录dml@2013.4.15   
  5.                  OfflineMessageStore oms = new OfflineMessageStore();  
  6.                  oms.addMessage_toHistory(packet);  
  7.                    
  8.              }  
  9.              catch (Exception e) {  
  10.                 log.error("Failed to route packet: " + packet.toXML(), e);  
  11.                  routingFailed(recipientJID, packet);  
  12.              }  

3.修改OfflineMessageStore类,添加保存消息记录方法

[java]  view plain copy print ?
  1. /** 
  2.  * $RCSfile$ 
  3.  * $Revision: 2911 $ 
  4.  * $Date: 2005-10-03 12:35:52 -0300 (Mon, 03 Oct 2005) $ 
  5.  * 
  6.  * Copyright (C) 2004-2008 Jive Software. All rights reserved. 
  7.  * 
  8.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  9.  * you may not use this file except in compliance with the License. 
  10.  * You may obtain a copy of the License at 
  11.  * 
  12.  *     http://www.apache.org/licenses/LICENSE-2.0 
  13.  * 
  14.  * Unless required by applicable law or agreed to in writing, software 
  15.  * distributed under the License is distributed on an "AS IS" BASIS, 
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  17.  * See the License for the specific language governing permissions and 
  18.  * limitations under the License. 
  19.  */  
  20.   
  21. package org.jivesoftware.openfire;  
  22.   
  23. import java.io.StringReader;  
  24. import java.sql.Connection;  
  25. import java.sql.PreparedStatement;  
  26. import java.sql.ResultSet;  
  27. import java.text.SimpleDateFormat;  
  28. import java.util.ArrayList;  
  29. import java.util.Collection;  
  30. import java.util.Date;  
  31. import java.util.List;  
  32. import java.util.Map;  
  33. import java.util.concurrent.BlockingQueue;  
  34. import java.util.concurrent.LinkedBlockingQueue;  
  35. import java.util.regex.Matcher;  
  36. import java.util.regex.Pattern;  
  37.   
  38. import org.dom4j.DocumentException;  
  39. import org.dom4j.Element;  
  40. import org.dom4j.io.SAXReader;  
  41. import org.jivesoftware.database.DbConnectionManager;  
  42. import org.jivesoftware.database.SequenceManager;  
  43. import org.jivesoftware.openfire.container.BasicModule;  
  44. import org.jivesoftware.openfire.event.UserEventDispatcher;  
  45. import org.jivesoftware.openfire.event.UserEventListener;  
  46. import org.jivesoftware.openfire.user.User;  
  47. import org.jivesoftware.openfire.user.UserManager;  
  48. import org.jivesoftware.util.JiveConstants;  
  49. import org.jivesoftware.util.LocaleUtils;  
  50. import org.jivesoftware.util.StringUtils;  
  51. import org.jivesoftware.util.XMPPDateTimeFormat;  
  52. import org.jivesoftware.util.cache.Cache;  
  53. import org.jivesoftware.util.cache.CacheFactory;  
  54. import org.slf4j.Logger;  
  55. import org.slf4j.LoggerFactory;  
  56. import org.xmpp.packet.JID;  
  57. import org.xmpp.packet.Message;  
  58.   
  59. /** 
  60.  * Represents the user's offline message storage. A message store holds messages 
  61.  * that were sent to the user while they were unavailable. The user can retrieve 
  62.  * their messages by setting their presence to "available". The messages will 
  63.  * then be delivered normally. Offline message storage is optional, in which 
  64.  * case a null implementation is returned that always throws 
  65.  * UnauthorizedException when adding messages to the store. 
  66.  *  
  67.  * @author Iain Shigeoka 
  68.  */  
  69. public class OfflineMessageStore extends BasicModule implements  
  70.         UserEventListener {  
  71.   
  72.     private static final Logger Log = LoggerFactory  
  73.             .getLogger(OfflineMessageStore.class);  
  74.     // 保存消息记录 dml@2013.4.16   
  75.     private static final String INSERT_HISTORY = "INSERT INTO ofHistory (username, messageID, creationDate, messageSize, stanza) "  
  76.             + "VALUES (?, ?, ?, ?, ?)";  
  77.   
  78.     private static final String INSERT_OFFLINE = "INSERT INTO ofOffline (username, messageID, creationDate, messageSize, stanza) "  
  79.             + "VALUES (?, ?, ?, ?, ?)";  
  80.     private static final String LOAD_OFFLINE = "SELECT stanza, creationDate FROM ofOffline WHERE username=?";  
  81.     private static final String LOAD_OFFLINE_MESSAGE = "SELECT stanza FROM ofOffline WHERE username=? AND creationDate=?";  
  82.     private static final String SELECT_SIZE_OFFLINE = "SELECT SUM(messageSize) FROM ofOffline WHERE username=?";  
  83.     private static final String SELECT_SIZE_ALL_OFFLINE = "SELECT SUM(messageSize) FROM ofOffline";  
  84.     private static final String DELETE_OFFLINE = "DELETE FROM ofOffline WHERE username=?";  
  85.     private static final String DELETE_OFFLINE_MESSAGE = "DELETE FROM ofOffline WHERE username=? AND creationDate=?";  
  86.   
  87.     private static final int POOL_SIZE = 10;  
  88.   
  89.     private Cache<String, Integer> sizeCache;  
  90.   
  91.     /** 
  92.      * Pattern to use for detecting invalid XML characters. Invalid XML 
  93.      * characters will be removed from the stored offline messages. 
  94.      */  
  95.     private Pattern pattern = Pattern.compile("&\\#[\\d]+;");  
  96.   
  97.     /** 
  98.      * Returns the instance of <tt>OfflineMessageStore</tt> being used by the 
  99.      * XMPPServer. 
  100.      *  
  101.      * @return the instance of <tt>OfflineMessageStore</tt> being used by the 
  102.      *         XMPPServer. 
  103.      */  
  104.     public static OfflineMessageStore getInstance() {  
  105.         return XMPPServer.getInstance().getOfflineMessageStore();  
  106.     }  
  107.   
  108.     /** 
  109.      * Pool of SAX Readers. SAXReader is not thread safe so we need to have a 
  110.      * pool of readers. 
  111.      */  
  112.     private BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<SAXReader>(  
  113.             POOL_SIZE);  
  114.   
  115.     /** 
  116.      * Constructs a new offline message store. 
  117.      */  
  118.     public OfflineMessageStore() {  
  119.         super("Offline Message Store");  
  120.         sizeCache = CacheFactory.createCache("Offline Message Size");  
  121.     }  
  122.   
  123.     /** 
  124.      * Adds a message to this message store. Messages will be stored and made 
  125.      * available for later delivery. 
  126.      *  
  127.      * @param message 
  128.      *            the message to store. 
  129.      */  
  130.     public void addMessage(Message message) {  
  131.         if (message == null) {  
  132.             return;  
  133.         }  
  134.         // ignore empty bodied message (typically chat-state notifications).   
  135.         if (message.getBody() == null || message.getBody().length() == 0) {  
  136.             // allow empty pubsub messages (OF-191)   
  137.             if (message.getChildElement("event",  
  138.                     "http://jabber.org/protocol/pubsub#event") == null) {  
  139.                 return;  
  140.             }  
  141.         }  
  142.         JID recipient = message.getTo();  
  143.         String username = recipient.getNode();  
  144.         // If the username is null (such as when an anonymous user), don't   
  145.         // store.   
  146.         if (username == null  
  147.                 || !UserManager.getInstance().isRegisteredUser(recipient)) {  
  148.             return;  
  149.         } else if (!XMPPServer.getInstance().getServerInfo().getXMPPDomain()  
  150.                 .equals(recipient.getDomain())) {  
  151.             // Do not store messages sent to users of remote servers   
  152.             return;  
  153.         }  
  154.   
  155.         long messageID = SequenceManager.nextID(JiveConstants.OFFLINE);  
  156.   
  157.         // Get the message in XML format.   
  158.         String msgXML = message.getElement().asXML();  
  159.   
  160.         Connection con = null;  
  161.         PreparedStatement pstmt = null;  
  162.         try {  
  163.             con = DbConnectionManager.getConnection();  
  164.             pstmt = con.prepareStatement(INSERT_OFFLINE);  
  165.             pstmt.setString(1, username);  
  166.             pstmt.setLong(2, messageID);  
  167.             pstmt.setString(3, StringUtils.dateToMillis(new java.util.Date()));  
  168.             // SimpleDateFormat df = new   
  169.             // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  170.             // pstmt.setString(3, df.format(new Date()).toString());   
  171.   
  172.             pstmt.setInt(4, msgXML.length());  
  173.             pstmt.setString(5, msgXML);  
  174.             pstmt.executeUpdate();  
  175.         }  
  176.   
  177.         catch (Exception e) {  
  178.             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);  
  179.         } finally {  
  180.             DbConnectionManager.closeConnection(pstmt, con);  
  181.         }  
  182.   
  183.         // Update the cached size if it exists.   
  184.         if (sizeCache.containsKey(username)) {  
  185.             int size = sizeCache.get(username);  
  186.             size += msgXML.length();  
  187.             sizeCache.put(username, size);  
  188.         }  
  189.     }  
  190.   
  191.     /** 
  192.      * 保存消息记录 
  193.      *  
  194.      * @author dml 
  195.      * @param message 
  196.      */  
  197.     public void addMessage_toHistory(Message message) {  
  198.         if (message == null) {  
  199.             return;  
  200.         }  
  201.         // ignore empty bodied message (typically chat-state notifications).   
  202.         if (message.getBody() == null || message.getBody().length() == 0) {  
  203.             // allow empty pubsub messages (OF-191)   
  204.             if (message.getChildElement("event",  
  205.                     "http://jabber.org/protocol/pubsub#event") == null) {  
  206.                 return;  
  207.             }  
  208.         }  
  209.         JID recipient = message.getTo();  
  210.         String username = recipient.getNode();  
  211.         // If the username is null (such as when an anonymous user), don't   
  212.         // store.   
  213.         if (username == null  
  214.                 || !UserManager.getInstance().isRegisteredUser(recipient)) {  
  215.             return;  
  216.         } else if (!XMPPServer.getInstance().getServerInfo().getXMPPDomain()  
  217.                 .equals(recipient.getDomain())) {  
  218.             // Do not store messages sent to users of remote servers   
  219.             return;  
  220.         }  
  221.   
  222.         long messageID = SequenceManager.nextID(JiveConstants.OFFLINE);  
  223.   
  224.         // Get the message in XML format.   
  225.         String msgXML = message.getElement().asXML();  
  226.   
  227.         Connection con = null;  
  228.         PreparedStatement pstmt = null;  
  229.         try {  
  230.             con = DbConnectionManager.getConnection();  
  231.             pstmt = con.prepareStatement(INSERT_HISTORY);  
  232.             pstmt.setString(1, username);  
  233.             pstmt.setLong(2, messageID);  
  234.             // pstmt.setString(3, StringUtils.dateToMillis(new   
  235.             // java.util.Date()));   
  236.             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  237.             pstmt.setString(3, df.format(new Date()).toString());  
  238.   
  239.             pstmt.setInt(4, msgXML.length());  
  240.             pstmt.setString(5, msgXML);  
  241.             pstmt.executeUpdate();  
  242.         }  
  243.   
  244.         catch (Exception e) {  
  245.             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);  
  246.         } finally {  
  247.             DbConnectionManager.closeConnection(pstmt, con);  
  248.         }  
  249.   
  250.         // Update the cached size if it exists.   
  251.         if (sizeCache.containsKey(username)) {  
  252.             int size = sizeCache.get(username);  
  253.             size += msgXML.length();  
  254.             sizeCache.put(username, size);  
  255.         }  
  256.     }  
  257.   
  258.     /** 
  259.      * Returns a Collection of all messages in the store for a user. Messages 
  260.      * may be deleted after being selected from the database depending on the 
  261.      * delete param. 
  262.      *  
  263.      * @param username 
  264.      *            the username of the user who's messages you'd like to receive. 
  265.      * @param delete 
  266.      *            true if the offline messages should be deleted. 
  267.      * @return An iterator of packets containing all offline messages. 
  268.      */  
  269.     public Collection<OfflineMessage> getMessages(String username,  
  270.             boolean delete) {  
  271.         List<OfflineMessage> messages = new ArrayList<OfflineMessage>();  
  272.         SAXReader xmlReader = null;  
  273.         Connection con = null;  
  274.         PreparedStatement pstmt = null;  
  275.         ResultSet rs = null;  
  276.         try {  
  277.             // Get a sax reader from the pool   
  278.             xmlReader = xmlReaders.take();  
  279.             con = DbConnectionManager.getConnection();  
  280.             pstmt = con.prepareStatement(LOAD_OFFLINE);  
  281.             pstmt.setString(1, username);  
  282.             rs = pstmt.executeQuery();  
  283.             while (rs.next()) {  
  284.                 String msgXML = rs.getString(1);  
  285.                 // 解析时间eg.Tue Apr 16 15:32:39 CST 2013   
  286.                 Date creationDate = new Date(Long.parseLong(rs.getString(2)  
  287.                         .trim()));  
  288.                 OfflineMessage message;  
  289.                 try {  
  290.                     message = new OfflineMessage(creationDate, xmlReader.read(  
  291.                             new StringReader(msgXML)).getRootElement());  
  292.                 } catch (DocumentException e) {  
  293.                     // Try again after removing invalid XML chars (e.g.  )   
  294.                     Matcher matcher = pattern.matcher(msgXML);  
  295.                     if (matcher.find()) {  
  296.                         msgXML = matcher.replaceAll("");  
  297.                     }  
  298.                     message = new OfflineMessage(creationDate, xmlReader.read(  
  299.                             new StringReader(msgXML)).getRootElement());  
  300.                 }  
  301.   
  302.                 // Add a delayed delivery (XEP-0203) element to the message.   
  303.                 Element delay = message.addChildElement("delay",  
  304.                         "urn:xmpp:delay");  
  305.                 delay.addAttribute("from", XMPPServer.getInstance()  
  306.                         .getServerInfo().getXMPPDomain());  
  307.                 delay.addAttribute("stamp",  
  308.                         XMPPDateTimeFormat.format(creationDate));  
  309.                 // Add a legacy delayed delivery (XEP-0091) element to the   
  310.                 // message. XEP is obsolete and support should be dropped in   
  311.                 // future.   
  312.                 delay = message.addChildElement("x""jabber:x:delay");  
  313.                 delay.addAttribute("from", XMPPServer.getInstance()  
  314.                         .getServerInfo().getXMPPDomain());  
  315.                 delay.addAttribute("stamp",  
  316.                         XMPPDateTimeFormat.formatOld(creationDate));  
  317.                 messages.add(message);  
  318.             }  
  319.             // Check if the offline messages loaded should be deleted, and that   
  320.             // there are   
  321.             // messages to delete.   
  322.             if (delete && !messages.isEmpty()) {  
  323.                 PreparedStatement pstmt2 = null;  
  324.                 try {  
  325.                     pstmt2 = con.prepareStatement(DELETE_OFFLINE);  
  326.                     pstmt2.setString(1, username);  
  327.                     pstmt2.executeUpdate();  
  328.                     removeUsernameFromSizeCache(username);  
  329.                 } catch (Exception e) {  
  330.                     Log.error("Error deleting offline messages of username: "  
  331.                             + username, e);  
  332.                 } finally {  
  333.                     DbConnectionManager.closeStatement(pstmt2);  
  334.                 }  
  335.             }  
  336.         } catch (Exception e) {  
  337.             Log.error("Error retrieving offline messages of username: "  
  338.                     + username, e);  
  339.         } finally {  
  340.             DbConnectionManager.closeConnection(rs, pstmt, con);  
  341.             // Return the sax reader to the pool   
  342.             if (xmlReader != null) {  
  343.                 xmlReaders.add(xmlReader);  
  344.             }  
  345.         }  
  346.         return messages;  
  347.     }  
  348.   
  349.     /** 
  350.      * Returns the offline message of the specified user with the given creation 
  351.      * date. The returned message will NOT be deleted from the database. 
  352.      *  
  353.      * @param username 
  354.      *            the username of the user who's message you'd like to receive. 
  355.      * @param creationDate 
  356.      *            the date when the offline message was stored in the database. 
  357.      * @return the offline message of the specified user with the given creation 
  358.      *         stamp. 
  359.      */  
  360.     public OfflineMessage getMessage(String username, Date creationDate) {  
  361.         OfflineMessage message = null;  
  362.         Connection con = null;  
  363.         PreparedStatement pstmt = null;  
  364.         ResultSet rs = null;  
  365.         SAXReader xmlReader = null;  
  366.         try {  
  367.             // Get a sax reader from the pool   
  368.             xmlReader = xmlReaders.take();  
  369.             con = DbConnectionManager.getConnection();  
  370.             pstmt = con.prepareStatement(LOAD_OFFLINE_MESSAGE);  
  371.             pstmt.setString(1, username);  
  372.             pstmt.setString(2, StringUtils.dateToMillis(creationDate));  
  373.             rs = pstmt.executeQuery();  
  374.             while (rs.next()) {  
  375.                 String msgXML = rs.getString(1);  
  376.                 message = new OfflineMessage(creationDate, xmlReader.read(  
  377.                         new StringReader(msgXML)).getRootElement());  
  378.                 // Add a delayed delivery (XEP-0203) element to the message.   
  379.                 Element delay = message.addChildElement("delay",  
  380.                         "urn:xmpp:delay");  
  381.                 delay.addAttribute("from", XMPPServer.getInstance()  
  382.                         .getServerInfo().getXMPPDomain());  
  383.                 delay.addAttribute("stamp",  
  384.                         XMPPDateTimeFormat.format(creationDate));  
  385.                 // Add a legacy delayed delivery (XEP-0091) element to the   
  386.                 // message. XEP is obsolete and support should be dropped in   
  387.                 // future.   
  388.                 delay = message.addChildElement("x""jabber:x:delay");  
  389.                 delay.addAttribute("from", XMPPServer.getInstance()  
  390.                         .getServerInfo().getXMPPDomain());  
  391.                 delay.addAttribute("stamp",  
  392.                         XMPPDateTimeFormat.formatOld(creationDate));  
  393.             }  
  394.         } catch (Exception e) {  
  395.             Log.error("Error retrieving offline messages of username: "  
  396.                     + username + " creationDate: " + creationDate, e);  
  397.         } finally {  
  398.             // Return the sax reader to the pool   
  399.             if (xmlReader != null) {  
  400.                 xmlReaders.add(xmlReader);  
  401.             }  
  402.             DbConnectionManager.closeConnection(rs, pstmt, con);  
  403.         }  
  404.         return message;  
  405.     }  
  406.   
  407.     /** 
  408.      * Deletes all offline messages in the store for a user. 
  409.      *  
  410.      * @param username 
  411.      *            the username of the user who's messages are going to be 
  412.      *            deleted. 
  413.      */  
  414.     public void deleteMessages(String username) {  
  415.         Connection con = null;  
  416.         PreparedStatement pstmt = null;  
  417.         try {  
  418.             con = DbConnectionManager.getConnection();  
  419.             pstmt = con.prepareStatement(DELETE_OFFLINE);  
  420.             pstmt.setString(1, username);  
  421.             pstmt.executeUpdate();  
  422.   
  423.             removeUsernameFromSizeCache(username);  
  424.         } catch (Exception e) {  
  425.             Log.error("Error deleting offline messages of username: "  
  426.                     + username, e);  
  427.         } finally {  
  428.             DbConnectionManager.closeConnection(pstmt, con);  
  429.         }  
  430.     }  
  431.   
  432.     private void removeUsernameFromSizeCache(String username) {  
  433.         // Update the cached size if it exists.   
  434.         if (sizeCache.containsKey(username)) {  
  435.             sizeCache.remove(username);  
  436.         }  
  437.     }  
  438.   
  439.     /** 
  440.      * Deletes the specified offline message in the store for a user. The way to 
  441.      * identify the message to delete is based on the creationDate and username. 
  442.      *  
  443.      * @param username 
  444.      *            the username of the user who's message is going to be deleted. 
  445.      * @param creationDate 
  446.      *            the date when the offline message was stored in the database. 
  447.      */  
  448.     public void deleteMessage(String username, Date creationDate) {  
  449.         Connection con = null;  
  450.         PreparedStatement pstmt = null;  
  451.         try {  
  452.             con = DbConnectionManager.getConnection();  
  453.             pstmt = con.prepareStatement(DELETE_OFFLINE_MESSAGE);  
  454.             pstmt.setString(1, username);  
  455.             pstmt.setString(2, StringUtils.dateToMillis(creationDate));  
  456.             pstmt.executeUpdate();  
  457.   
  458.             // Force a refresh for next call to getSize(username),   
  459.             // it's easier than loading the message to be deleted just   
  460.             // to update the cache.   
  461.             removeUsernameFromSizeCache(username);  
  462.         } catch (Exception e) {  
  463.             Log.error("Error deleting offline messages of username: "  
  464.                     + username + " creationDate: " + creationDate, e);  
  465.         } finally {  
  466.             DbConnectionManager.closeConnection(pstmt, con);  
  467.         }  
  468.     }  
  469.   
  470.     /** 
  471.      * Returns the approximate size (in bytes) of the XML messages stored for a 
  472.      * particular user. 
  473.      *  
  474.      * @param username 
  475.      *            the username of the user. 
  476.      * @return the approximate size of stored messages (in bytes). 
  477.      */  
  478.     public int getSize(String username) {  
  479.         // See if the size is cached.   
  480.         if (sizeCache.containsKey(username)) {  
  481.             return sizeCache.get(username);  
  482.         }  
  483.         int size = 0;  
  484.         Connection con = null;  
  485.         PreparedStatement pstmt = null;  
  486.         ResultSet rs = null;  
  487.         try {  
  488.             con = DbConnectionManager.getConnection();  
  489.             pstmt = con.prepareStatement(SELECT_SIZE_OFFLINE);  
  490.             pstmt.setString(1, username);  
  491.             rs = pstmt.executeQuery();  
  492.             if (rs.next()) {  
  493.                 size = rs.getInt(1);  
  494.             }  
  495.             // Add the value to cache.   
  496.             sizeCache.put(username, size);  
  497.         } catch (Exception e) {  
  498.             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);  
  499.         } finally {  
  500.             DbConnectionManager.closeConnection(rs, pstmt, con);  
  501.         }  
  502.         return size;  
  503.     }  
  504.   
  505.     /** 
  506.      * Returns the approximate size (in bytes) of the XML messages stored for 
  507.      * all users. 
  508.      *  
  509.      * @return the approximate size of all stored messages (in bytes). 
  510.      */  
  511.     public int getSize() {  
  512.         int size = 0;  
  513.         Connection con = null;  
  514.         PreparedStatement pstmt = null;  
  515.         ResultSet rs = null;  
  516.         try {  
  517.             con = DbConnectionManager.getConnection();  
  518.             pstmt = con.prepareStatement(SELECT_SIZE_ALL_OFFLINE);  
  519.             rs = pstmt.executeQuery();  
  520.             if (rs.next()) {  
  521.                 size = rs.getInt(1);  
  522.             }  
  523.         } catch (Exception e) {  
  524.             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);  
  525.         } finally {  
  526.             DbConnectionManager.closeConnection(rs, pstmt, con);  
  527.         }  
  528.         return size;  
  529.     }  
  530.   
  531.     public void userCreated(User user, Map params) {  
  532.         // Do nothing   
  533.     }  
  534.   
  535.     public void userDeleting(User user, Map params) {  
  536.         // Delete all offline messages of the user   
  537.         deleteMessages(user.getUsername());  
  538.     }  
  539.   
  540.     public void userModified(User user, Map params) {  
  541.         // Do nothing   
  542.     }  
  543.   
  544.     @Override  
  545.     public void start() throws IllegalStateException {  
  546.         super.start();  
  547.         // Initialize the pool of sax readers   
  548.         for (int i = 0; i < POOL_SIZE; i++) {  
  549.             SAXReader xmlReader = new SAXReader();  
  550.             xmlReader.setEncoding("UTF-8");  
  551.             xmlReaders.add(xmlReader);  
  552.         }  
  553.         // Add this module as a user event listener so we can delete   
  554.         // all offline messages when a user is deleted   
  555.         UserEventDispatcher.addListener(this);  
  556.     }  
  557.   
  558.     @Override  
  559.     public void stop() {  
  560.         super.stop();  
  561.         // Clean up the pool of sax readers   
  562.         xmlReaders.clear();  
  563.         // Remove this module as a user event listener   
  564.         UserEventDispatcher.removeListener(this);  
  565.     }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值