activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r368560 [2/2] - in /incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker: ./ console/
Date Fri, 13 Jan 2006 02:53:09 GMT
Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java?rev=368560&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java
Thu Jan 12 18:52:57 2006
@@ -0,0 +1,132 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.broker.console;
+
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.ObjectInstance;
+import java.util.List;
+import java.util.Set;
+import java.util.Iterator;
+import java.util.HashSet;
+
+public class ShutdownTask extends AbstractJmxTask {
+    private boolean isStopAllBrokers = false;
+
+    protected void startTask(List brokerNames) {
+        try {
+            Set mbeans = new HashSet();
+            MBeanServerConnection server = createJmxConnector().getMBeanServerConnection();
+
+            // Stop all brokers
+            if (isStopAllBrokers) {
+                mbeans = AmqJmxSupport.getAllBrokers(server);
+                brokerNames.clear();
+            }
+
+            // Stop the default broker
+            else if (brokerNames.isEmpty()) {
+                mbeans = AmqJmxSupport.getAllBrokers(server);
+
+                // If there is no broker to stop
+                if (mbeans.isEmpty()) {
+                    System.out.println("There are no brokers to stop.");
+                    return;
+
+                // There should only be one broker to stop
+                } else if (mbeans.size() > 1) {
+                    System.out.println("There are multiple brokers to stop. Please select
the broker(s) to stop or use --all to stop all brokers.");
+                    System.out.println();
+                    AmqJmxSupport.printBrokerList(mbeans);
+                    return;
+
+                // Get the first broker only
+                } else {
+                    Object firstBroker = mbeans.iterator().next();
+                    mbeans.clear();
+                    mbeans.add(firstBroker);
+                }
+            }
+
+            // Stop each specified broker
+            else {
+                String brokerName;
+                while (!brokerNames.isEmpty()) {
+                    brokerName = (String)brokerNames.remove(0);
+                    Set matchedBrokers = AmqJmxSupport.getBrokers(server, brokerName);
+                    if (matchedBrokers.isEmpty()) {
+                        System.out.println(brokerName + " did not match any running brokers.");
+                    } else {
+                        mbeans.addAll(matchedBrokers);
+                    }
+                }
+            }
+
+            // Stop all brokers in set
+            stopBrokers(server, mbeans);
+            
+            closeJmxConnector();
+        } catch (Throwable e) {
+            System.out.println("Failed to execute stop task. Reason: " + e);
+        }
+    }
+
+    protected void stopBrokers(MBeanServerConnection server, Set brokerBeans) throws Exception
{
+        ObjectName brokerObjName;
+        for (Iterator i=brokerBeans.iterator(); i.hasNext();) {
+            brokerObjName = ((ObjectInstance)i.next()).getObjectName();
+
+            String brokerName = brokerObjName.getKeyProperty("BrokerName");
+            System.out.println("Stopping broker: " + brokerName);
+
+            try {
+                server.invoke(brokerObjName, "terminateJVM", new Object[] {new Integer(0)},
new String[] {"int"});
+                System.out.println("Succesfully stopped broker: " + brokerName);
+            } catch (Exception e) {
+                // TODO: Check exceptions throwned
+                //System.out.println("Failed to stop broker: [ " + brokerName + " ]. Reason:
" + e.getMessage());
+            }
+        }
+    }
+
+    protected void printHelp() {
+        System.out.println("Task Usage: Main stop [stop-options] [broker-name1] [broker-name2]
...");
+        System.out.println("Description: Stops a running broker.");
+        System.out.println("");
+        System.out.println("Stop Options:");
+        System.out.println("    --jmxurl <url>      Set the JMX URL to connect to.");
+        System.out.println("    --all               Stop all brokers.");
+        System.out.println("    --version           Display the version information.");
+        System.out.println("    -h,-?,--help        Display the stop broker help information.");
+        System.out.println("");
+        System.out.println("Broker Names:");
+        System.out.println("    Name of the brokers that will be stopped.");
+        System.out.println("    If omitted, it is assumed that there is only one broker running,
and it will be stopped.");
+        System.out.println("    Use -all to stop all running brokers.");
+        System.out.println("");
+    }
+
+    protected void handleOption(String token, List tokens) throws Exception {
+        // Try to handle the options first
+        if (token.equals("--all")) {
+            isStopAllBrokers = true;
+        } else {
+            // Let the super class handle the option
+            super.handleOption(token, tokens);
+        }
+    }
+}

Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java?rev=368560&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java
Thu Jan 12 18:52:57 2006
@@ -0,0 +1,163 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.broker.console;
+
+import org.apache.activemq.broker.BrokerFactory;
+import org.apache.activemq.broker.BrokerService;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+public class StartTask extends AbstractTask {
+
+    public static final String DEFAULT_CONFIG_URI   = "xbean:activemq.xml";
+
+    private URI  configURI;
+    private List brokers = new ArrayList(5);
+
+    /**
+     * The default task to start a broker or a group of brokers
+     * @param brokerURIs
+     */
+    protected void startTask(List brokerURIs) {
+        try {
+            // If no config uri, use default setting
+            if (brokerURIs.isEmpty()) {
+                setConfigUri(new URI(DEFAULT_CONFIG_URI));
+                startBroker(getConfigUri());
+
+            // Set configuration data, if available, which in this case would be the config
URI
+            } else {
+                String strConfigURI;
+
+                while (!brokerURIs.isEmpty()) {
+                    strConfigURI = (String)brokerURIs.remove(0);
+
+                    try {
+                        setConfigUri(new URI(strConfigURI));
+                    } catch (URISyntaxException e) {
+                        printError("Invalid broker configuration URI: " + strConfigURI +
", reason: " + e.getMessage());
+                        return;
+                    }
+
+                    startBroker(getConfigUri());
+                }
+            }
+
+            // Prevent the main thread from exiting unless it is terminated elsewhere
+            waitForShutdown();
+        } catch (Throwable e) {
+            System.out.println("Failed to execute start task. Reason: " + e);
+        }
+    }
+
+    /**
+     * Create and run a broker specified by the given configuration URI
+     * @param configURI
+     * @throws Exception
+     */
+    public void startBroker(URI configURI) throws Exception {
+        System.out.println("Loading message broker from: " + configURI);
+        BrokerService broker = BrokerFactory.createBroker(configURI);
+        brokers.add(broker);
+
+        broker.start();
+    }
+
+    /**
+     * Wait for a shutdown invocation elsewhere
+     * @throws Exception
+     */
+    protected void waitForShutdown() throws Exception {
+        final boolean[] shutdown = new boolean[] {false};
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            public void run() {
+                synchronized(shutdown) {
+                    shutdown[0]=true;
+                    shutdown.notify();
+                }
+            }
+        });
+
+        // Wait for any shutdown event
+        synchronized(shutdown) {
+            while( !shutdown[0] ) {
+                try {
+                    shutdown.wait();
+                } catch (InterruptedException e) {
+                }
+            }
+        }
+
+        // Stop each broker
+        for (Iterator i=brokers.iterator(); i.hasNext();) {
+            BrokerService broker = (BrokerService)i.next();
+            broker.stop();
+        }
+    }
+
+    /**
+     * Prints the help for the start broker task
+     */
+    protected void printHelp() {
+        System.out.println("Task Usage: Main start [start-options] [uri]");
+        System.out.println("Description: Creates and starts a broker using a configuration
file, or a broker URI.");
+        System.out.println("");
+        System.out.println("Start Options:");
+        System.out.println("    --extdir <dir>        Add the jar files in the directory
to the classpath.");
+        System.out.println("    -D<name>=<value>      Define a system property.");
+        System.out.println("    --version             Display the version information.");
+        System.out.println("    -h,-?,--help          Display the start broker help information.");
+        System.out.println("");
+        System.out.println("URI:");
+        System.out.println("");
+        System.out.println("    XBean based broker configuration:");
+        System.out.println("");
+        System.out.println("        Example: Main xbean:file:activemq.xml");
+        System.out.println("            Loads the xbean configuration file from the current
working directory");
+        System.out.println("        Example: Main xbean:activemq.xml");
+        System.out.println("            Loads the xbean configuration file from the classpath");
+        System.out.println("");
+        System.out.println("    URI Parameter based broker configuration:");
+        System.out.println("");
+        System.out.println("        Example: Main broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true");
+        System.out.println("            Configures the broker with 2 transport connectors
and jmx enabled");
+        System.out.println("        Example: Main broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false");
+        System.out.println("            Configures the broker with 1 transport connector,
and 1 network connector and persistence disabled");
+        System.out.println("");
+    }
+
+    /**
+     * Sets the current configuration URI used by the start task
+     * @param uri
+     */
+    public void setConfigUri(URI uri) {
+        configURI = uri;
+    }
+
+    /**
+     * Gets the current configuration URI used by the start task
+     * @return current configuration URI
+     */
+    public URI getConfigUri() {
+        return configURI;
+    }
+}

Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java?rev=368560&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java
Thu Jan 12 18:52:57 2006
@@ -0,0 +1,24 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.broker.console;
+
+import java.util.List;
+
+public interface Task {
+    public void runTask(List tokens) throws Exception;
+}



Mime
View raw message