1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activecluster;
19
20 import org.codehaus.activecluster.impl.ActiveMQClusterFactory;
21 import org.codehaus.activecluster.impl.DefaultClusterFactory;
22 import org.codehaus.activecluster.election.ElectionStrategy;
23 import org.codehaus.activecluster.election.impl.BullyElectionStrategy;
24
25 import javax.jms.JMSException;
26 import java.io.BufferedReader;
27 import java.io.InputStreamReader;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /***
32 * @version $Revision: 1.9 $
33 */
34 public class ClusterDemo {
35 protected Cluster cluster;
36 private String name;
37 private ElectionStrategy electionStrategy;
38
39 public static void main(String[] args) {
40 try {
41 ClusterDemo test = new ClusterDemo();
42 if (args.length > 0) {
43 test.name = args[0];
44 }
45 test.demo();
46 }
47 catch (JMSException e) {
48 System.out.println("Caught: " + e);
49 e.printStackTrace();
50 Exception c = e.getLinkedException();
51 if (c != null) {
52 System.out.println("Cause: " + c);
53 c.printStackTrace();
54 }
55 }
56 catch (Exception e) {
57 System.out.println("Caught: " + e);
58 e.printStackTrace();
59 }
60 }
61
62 public void demo() throws Exception {
63 start();
64
65 cluster.addClusterListener(new TestingClusterListener(cluster));
66
67 System.out.println("Enter 'quit' to terminate");
68 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
69 while (true) {
70 String line = reader.readLine();
71 if (line == null || line.trim().equalsIgnoreCase("quit")) {
72 break;
73 }
74 else {
75 Map map = new HashMap();
76 map.put("text", line);
77 cluster.getLocalNode().setState(map);
78 }
79 }
80 stop();
81 }
82
83
84 protected void start() throws JMSException, ClusterException {
85 cluster = createCluster();
86 if (name != null) {
87 System.out.println("Starting node: " + name);
88
89
90 Map state = new HashMap();
91 state.put("name", name);
92 cluster.getLocalNode().setState(state);
93 }
94 cluster.start();
95 if (electionStrategy == null) {
96 electionStrategy = new BullyElectionStrategy();
97 }
98 electionStrategy.doElection(cluster);
99 }
100
101 protected void stop() throws JMSException {
102 cluster.stop();
103 }
104
105 protected Cluster createCluster() throws JMSException, ClusterException {
106 ClusterFactory factory = new ActiveMQClusterFactory();
107 return factory.createCluster("ORG.CODEHAUS.ACTIVEMQ.TEST.CLUSTER");
108 }
109 }