abstractmessagehandler.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 461 行 · 第 1/2 页

JAVA
461
字号
     *     * @param messageId the message identifier     * @param message   the message to populate     * @throws JMSException         for any JMS error     * @throws PersistenceException for any persistence error     */    protected void getProperties(String messageId, Message message)            throws JMSException, PersistenceException {        Map properties = getProperties(messageId);        Iterator iterator = properties.entrySet().iterator();        while (iterator.hasNext()) {            Map.Entry entry = (Map.Entry) iterator.next();            String name = (String) entry.getKey();            Object value = entry.getValue();            message.setObjectProperty(name, value);        }    }    /**     * Add a message.     *     * @param message the message to add     * @param type    the type of the message     * @throws JMSException         for any JMS error     * @throws PersistenceException for any persistence error     */    protected void add(Message message, String type)            throws JMSException, PersistenceException {        PreparedStatement insert = null;        String messageId = null;        try {            insert = _connection.prepareStatement(                    "insert into " + MESSAGE_TABLE                    + " values  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");            messageId = message.getJMSMessageID();            long destinationId = _destinations.getId(                    (JmsDestination) message.getJMSDestination());                        // insert message header            insert.setString(1, messageId);            insert.setString(2, type);            insert.setString(3, message.getJMSCorrelationID());            insert.setInt(4, message.getJMSDeliveryMode());            insert.setLong(5, destinationId);            insert.setLong(6, message.getJMSExpiration());            insert.setInt(7, message.getJMSPriority());            insert.setBoolean(8, message.getJMSRedelivered());            long replyToId = 0;            if (message.getJMSReplyTo() != null) {                JmsDestination replyTo =                        (JmsDestination) message.getJMSReplyTo();                replyToId = _destinations.getId(replyTo);            }            insert.setLong(9, replyToId);            insert.setLong(10, message.getJMSTimestamp());            insert.setString(11, message.getJMSType());            Object body = getBody(message);            byte[] blob;            try {                blob = serialize(body);            } catch (Exception exception) {                throw new PersistenceException(                        "Failed to serialize message body, JMSMessageID="                        + messageId, exception);            }            insert.setObject(12, blob);            insert.executeUpdate();                        // insert header properties            Enumeration iterator = message.getPropertyNames();            while (iterator.hasMoreElements()) {                String name = (String) iterator.nextElement();                Object value = message.getObjectProperty(name);                addProperty(messageId, name, value);            }            _connection.commit();        } catch (SQLException exception) {            throw new PersistenceException(                    "Failed to add message, JMSMessageID=" + messageId,                    exception);        } finally {            SQLHelper.close(insert);        }    }    /**     * Returns properties for a message.     *     * @param messageId the message identifier     * @return a map of properties     * @throws PersistenceException for any persistence error     */    protected Map getProperties(String messageId)            throws PersistenceException {        HashMap result = new HashMap();        PreparedStatement select = null;        ResultSet set = null;        try {            select = _connection.prepareStatement(                    "select name, value from " + MESSAGE_PROPERTIES_TABLE                    + " where message_id = ?");            select.setString(1, messageId);            set = select.executeQuery();            while (set.next()) {                String name = set.getString("name");                Blob blob = set.getBlob("value");                Object value;                try {                    value = deserialize(blob);                } catch (Exception exception) {                    String message = "Failed to destream property for "                            + "message, JMSMessageID=" + messageId                            + ", property=" + name;                    throw new PersistenceException(message, exception);                }                result.put(name, value);            }        } catch (SQLException exception) {            throw new PersistenceException(                    "Failed to get properties for message, JMSMessageID="                    + messageId, exception);        } finally {            SQLHelper.close(set);            SQLHelper.close(select);        }        return result;    }    /**     * Add a property.     *     * @param messageId the message identifier     * @param name      the property name     * @param value     the property value     * @throws PersistenceException for any persistence error     */    protected void addProperty(String messageId,                               String name, Object value)            throws PersistenceException {        byte[] blob;        try {            blob = serialize(value);        } catch (IOException exception) {            String message = "Failed to serialize property for message, "                    + "JMSMessageID=" + messageId + ", name=" + name;            if (value != null) {                message += " of type " + value.getClass().getName();            }            throw new PersistenceException(message, exception);        }        PreparedStatement insert = null;        try {            insert = _connection.prepareStatement(                    "insert into " + MESSAGE_PROPERTIES_TABLE                    + " values (?, ?, ?)");            insert.setString(1, messageId);            insert.setString(2, name);            insert.setObject(3, blob);            insert.executeUpdate();        } catch (SQLException exception) {            throw new PersistenceException(                    "Failed to add property for message, JMSMessageID="                    + messageId + ", name=" + name + ", value=" + value,                    exception);        } finally {            SQLHelper.close(insert);        }    }    /**     * Helper to serialize an object to a byte array.     *     * @param object the object to serialize     * @return the serialized object     * @throws IOException if the object cannot be serialized     */    public byte[] serialize(Object object) throws IOException {        byte[] result;        ByteArrayOutputStream bstream = new ByteArrayOutputStream();        ObjectOutputStream ostream = new ObjectOutputStream(bstream);        ostream.writeObject(object);        ostream.close();        result = bstream.toByteArray();        return result;    }    /**     * Helper to deserialize an object from a byte array.     *     * @param blob the blob containing object to deserialize     * @return the destreamed object     * @throws ClassNotFoundException if the class of a serialized object cannot     *                                be found.     * @throws IOException            if the object cannot be deserialized     * @throws SQLException           if there is an error accessing the     *                                <code>blob</code>     */    protected Object deserialize(Blob blob)            throws ClassNotFoundException, IOException, SQLException {        Object result = null;        if (blob != null) {            ObjectInputStream istream = new ObjectInputStream(                    blob.getBinaryStream());            result = istream.readObject();            istream.close();        }        return result;    }    /**     * Returns the database connection.     *     * @return the connection to the database     */    protected Connection getConnection() {        return _connection;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?