Permalink
Browse files

Created the IBrokerServiceManager interface to allow for multiple

implementations to be used.

Added the EnhancedBrokerServiceProcess and EnhancedBrokerServiceManager
classes that are experimental version of the current implementation
that might also replace the current implementation.

The StateEnum class was added and is used by the new classes.
  • Loading branch information...
1 parent 29b491e commit 85c94da4fca8c841b32c5d91507af1822b958e40 @ccorsi committed Jul 31, 2012
View
@@ -117,5 +117,10 @@
<version>1.0.2</version>
<optional>false</optional>
</dependency>
+ <dependency>
+ <groupId>org.valhalla.tools.net</groupId>
+ <artifactId>net-proc-comm-tool</artifactId>
+ <version>1.0.0</version>
+ </dependency>
</dependencies>
</project>
@@ -43,7 +43,7 @@
private static final Logger logger = LoggerFactory.getLogger(AbstractBrokerServiceSpawner.class);
- private Collection<BrokerServiceManager> managers = new LinkedList<BrokerServiceManager>();
+ private Collection<IBrokerServiceManager> managers = new LinkedList<IBrokerServiceManager>();
private int startedBrokers = 0;
@@ -214,7 +214,7 @@ protected void addToJVMArgs(List<String> jvmArgs) {}
* @throws IOException
*/
public final void stopBrokers() throws IOException {
- for( BrokerServiceManager manager : managers) {
+ for( IBrokerServiceManager manager : managers) {
manager.stopBroker();
}
}
@@ -23,7 +23,7 @@
* @author Claudio Corsi
*
*/
-public class BrokerServiceManager {
+public class BrokerServiceManager implements IBrokerServiceManager {
private Socket socket;
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.brokerservice.runner;
+
+import java.io.IOException;
+
+import org.slf4j.LoggerFactory;
+import org.valhalla.tools.net.CommandExecutor;
+import org.valhalla.tools.net.server.ServiceServer;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class EnhancedBrokerServiceManager implements IBrokerServiceManager {
+
+ private ServiceServer<EnhancedBrokerServiceManager, EnhancedBrokerServiceProcess> server;
+ private StateEnum state = StateEnum.INITIALIZED;
+
+ public EnhancedBrokerServiceManager() throws IOException {
+ server = new ServiceServer<EnhancedBrokerServiceManager, EnhancedBrokerServiceProcess>() {
+
+ @Override
+ protected void serviceStarted() {
+ state = StateEnum.STARTED;
+ }
+
+ @Override
+ protected void serviceStopped() {
+ state = StateEnum.STOPPED;
+ }
+ };
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.activemq.brokerservice.runner.IBrokerServiceManager#stopBroker()
+ */
+ @Override
+ public void stopBroker() {
+ if (state == StateEnum.STARTED) {
+ // Send a stop message to the other process...
+ server.enqueue(new CommandExecutor<EnhancedBrokerServiceProcess, EnhancedBrokerServiceManager>() {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public CommandExecutor<EnhancedBrokerServiceManager, EnhancedBrokerServiceProcess> apply(
+ EnhancedBrokerServiceProcess process) {
+ try {
+ process.stopBroker();
+ } catch (Exception e) {
+ LoggerFactory.getLogger(this.getClass()).debug("An exception was raised", e);
+ }
+ return null;
+ }
+
+ });
+ }
+ }
+
+ /**
+ * This will be called by the remote process and used to set the current state to STOPPED.
+ */
+ public void brokerStopped() {
+ this.state = StateEnum.STOPPED;
+ }
+
+}
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.brokerservice.runner;
+
+import java.net.URI;
+
+import org.apache.activemq.broker.BrokerFactory;
+import org.apache.activemq.broker.BrokerService;
+import org.valhalla.tools.net.CommandExecutor;
+import org.valhalla.tools.net.client.ServiceClient;
+import org.valhalla.tools.net.server.ServiceServer;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class EnhancedBrokerServiceProcess {
+
+ private BrokerService broker;
+ private ServiceClient<EnhancedBrokerServiceProcess, EnhancedBrokerServiceManager> client;
+
+ public void execute() throws Exception {
+ client = new ServiceClient<EnhancedBrokerServiceProcess, EnhancedBrokerServiceManager>() {
+
+ @Override
+ public void startedServiceManager() {
+ // TODO Auto-generated method stub
+
+ }
+
+ };
+ client.enqueue(new CommandExecutor<EnhancedBrokerServiceManager, EnhancedBrokerServiceProcess>() {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public CommandExecutor<EnhancedBrokerServiceProcess, EnhancedBrokerServiceManager> apply(
+ EnhancedBrokerServiceManager destination) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ });
+ // This is the main method that will be used to start the embedded brokers.
+ // It will expect to find the broker.config.file property to be set.
+ String brokerConfigFile = System.getProperty("broker.config.file");
+ if (brokerConfigFile == null) {
+ // Raise an exception since no activemq configuration file was passed.
+ throw new RuntimeException("No activemq configuration file was passed");
+ }
+
+ System.out.println("STARTING BROKER USING " + brokerConfigFile + " configuration file.");
+ broker = BrokerFactory.createBroker(new URI(brokerConfigFile), true);
+ broker.waitUntilStarted();
+ }
+
+ public void stopBroker() throws Exception {
+ System.out.println("STOPPING BROKER...");
+ broker.stop();
+ System.out.println("WAITING FOR BROKER TO COMPLETELY STOP");
+ broker.waitUntilStopped();
+ System.out.println("BROKER HAS STOPPED");
+ client.enqueue(new CommandExecutor<EnhancedBrokerServiceManager, EnhancedBrokerServiceProcess>() {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public CommandExecutor<EnhancedBrokerServiceProcess, EnhancedBrokerServiceManager> apply(
+ EnhancedBrokerServiceManager manager) {
+ manager.brokerStopped();
+ return null;
+ }
+ });
+ }
+}
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.brokerservice.runner;
+
+import java.io.IOException;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public interface IBrokerServiceManager {
+
+ /**
+ *
+ */
+ void stopBroker() throws IOException;
+
+}
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.brokerservice.runner;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public enum StateEnum {
+ INITIALIZED, STARTED, STOPPED
+
+}

0 comments on commit 85c94da

Please sign in to comment.