adminconnection.java

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

JAVA
592
字号
    public boolean durableConsumerExists(String name) {        return (_consumers.getConsumerEndpoint(name) != null);    }    /**     * Return the collection of durable consumer names for a particular topic     * destination.     *     * @param topic the topic name     * @return Vector              collection of strings     */    public Vector getDurableConsumers(String topic) {        Enumeration iter = null;        Vector result = new Vector();        try {            _database.begin();            Connection connection = _database.getConnection();            iter = _database.getAdapter().getDurableConsumers(connection,                                                              topic);            // copy the elements into the vector            while (iter.hasMoreElements()) {                result.addElement(iter.nextElement());            }            _database.commit();        } catch (Exception exception) {            _log.error("Failed on get durable consumers for topic=" + topic,                       exception);            rollback();        }        return result;    }    /**     * De-Activate an active persistent consumer.     *     * @param name name of the consumer     * @return boolean             true if successful     */    public boolean unregisterConsumer(String name) {        boolean success = false;        ConsumerEndpoint endpoint = _consumers.getConsumerEndpoint(name);        if (endpoint != null) {            _consumers.closeConsumer(endpoint);        }        success = true;        return success;    }    /**     * Check to see if the given consumer is currently connected to the     * OpenJMSServer. This is only valid when in online mode.     *     * @param name The name of the onsumer.     * @return boolean True if the consumer is connected.     */    public boolean isConnected(String name) {        boolean result = false;        ConsumerEndpoint endpoint = _consumers.getConsumerEndpoint(name);        if (endpoint != null && endpoint instanceof DurableConsumerEndpoint) {            result = ((DurableConsumerEndpoint) endpoint).isActive();        }        return result;    }    /**     * Return a list of all registered destinations.     *     * @return Vector     collection of strings     */    public Vector getAllDestinations() {        Enumeration iter = null;        Vector result = new Vector();        try {            _database.begin();            Connection connection = _database.getConnection();            iter = _database.getAdapter().getAllDestinations(connection);            // copy the elements into the vector            while (iter.hasMoreElements()) {                result.addElement(iter.nextElement());            }            _database.commit();        } catch (Exception exception) {            _log.error("Failed to get all destinations", exception);            rollback();        }        return result;    }    /**     * Add an administered destination with the specified name.     *     * @param name  destination name     * @param queue whether it is queue or a topic     * @return boolean             true if successful     */    public boolean addDestination(String name, Boolean queue) {        boolean success = false;        // create the appropriate destination object        JmsDestination destination = (queue.booleanValue())                ? (JmsDestination) new JmsQueue(name)                : (JmsDestination) new JmsTopic(name);        destination.setPersistent(true);        // create the administered destination        try {            if (_destinations.getDestination(name) == null) {                _destinations.createDestination(destination);                success = true;            }        } catch (JMSException exception) {            _log.error("Failed to add destination=" + name, exception);        }        return success;    }    /**     * Destroy the specified destination and all associated messsages and     * consumers. This is a very dangerous operation to execute while there are     * clients online     *     * @param name destination to destroy     * @return boolean             true if successful     */    public boolean removeDestination(String name) {        boolean success = false;        JmsDestination dest = _destinations.getDestination(name);        // ensure that the destination actually translates to a valid        // object.        if (dest != null) {            try {                _destinations.removeDestination(dest);                success = true;            } catch (JMSException exception) {                _log.error("Failed to remove destination=" + name, exception);            }        }        return success;    }    /**     * Check whether the specified destination exists     *     * @param name - the name of the destination to check     * @return boolean - true if it does and false otherwise     */    public boolean destinationExists(String name) {        JmsDestination dest = _destinations.getDestination(name);        return (dest != null);    }    /**     * Terminate the JMS Server. If it is running as a standalone application     * then exit the application. It is running as an embedded application then     * just terminate the thread     */    public void stopServer() {        boolean isEmbedded = false;        Connector[] connectors = _config.getConnectors().getConnector();        for (int i = 0; i < connectors.length; ++i) {            if (connectors[i].getScheme().equals(SchemeType.EMBEDDED)) {                isEmbedded = true;                break;            }        }        final boolean exit = !isEmbedded;        Runnable r = new Runnable() {            public void run() {                try {                    // give the caller a chance to return before shutting                    // down services                    Thread.sleep(1000);                } catch (InterruptedException ignore) {                }                _log.info("Stopping all services");                try {                    _services.stop();                } catch (ServiceException exception) {                    _log.error(exception, exception);                }                if (exit) {                    _log.info("Server shutdown scheduled for 5 secs");                    try {                        Thread.sleep(5000);                    } catch (InterruptedException ignore) {                    }                    System.exit(0);                }            }        };        Thread t = new Thread(r);        t.start();    }    /**     * Purge all processed messages from the database     *     * @return int         number of messages purged     */    public int purgeMessages() {        return _database.getAdapter().purgeMessages();    }    /**     * Add a user with the specified name     *     * @param username the users name     * @param password the users password     * @return <code>true</code> if the user is added otherwise     *         <code>false</code>     */    public boolean addUser(String username, String password) {        return _authenticator.addUser(new User(username, password));    }    /**     * Change password for the specified user     *     * @param username the users name     * @param password the users password     * @return <code>true</code> if the password is changed otherwise     *         <code>false</code>     */    public boolean changePassword(String username, String password) {        return _authenticator.updateUser(new User(username, password));    }    /**     * Remove the specified user     *     * @param username the users name     * @return <code>true</code> if the user is removed otherwise     *         <code>false</code>     */    public boolean removeUser(String username) {        return _authenticator.removeUser(new User(username, null));    }    /**     * Return a list of all registered users.     *     * @return Vector of users     */    public Vector getAllUsers() {        Enumeration iter = null;        Vector result = new Vector();        try {            _database.begin();            Connection connection = _database.getConnection();            iter = _database.getAdapter().getAllUsers(connection);            // copy the elements into the vector            while (iter.hasMoreElements()) {                result.addElement(iter.nextElement());            }            _database.commit();        } catch (Exception exception) {            _log.error("Failed on get all users", exception);            rollback();        }        return result;    }    /**     * Rollback the current transaction, logging any error.     */    private void rollback() {        try {            _database.rollback();        } catch (PersistenceException exception) {            _log.error(exception, exception);        }    }}

⌨️ 快捷键说明

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