forked from hornetq/hornetq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HornetQExample.java
224 lines (195 loc) · 7.65 KB
/
HornetQExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2009 Red Hat, Inc.
* Red Hat 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.hornetq.common.example;
import java.io.File;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.naming.InitialContext;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.api.jms.JMSFactoryType;
import org.hornetq.core.client.impl.DelegatingSession;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
import org.hornetq.jms.client.HornetQConnection;
import org.hornetq.jms.client.HornetQConnectionFactory;
/**
* Base class for HornetQ examples.
* <p>
* This takes care of starting and stopping the server as well as deploying any
* queue needed.
*
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
*/
public abstract class HornetQExample
{
protected static final Logger log = Logger.getLogger(HornetQExample.class
.getName());
protected boolean failure = false;
private String[] args;
public abstract boolean runExample() throws Exception;
protected void run(final String[] args1)
{
this.args = args1;
//if we have a cluster of servers wait a while for the cluster to form properly
if(args != null && args.length > 1)
{
System.out.println("****pausing to allow cluster to form****");
Thread.yield();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
//ignore
}
}
try
{
if (!runExample())
{
failure = true;
}
System.out.println("example complete");
}
catch (Throwable e)
{
failure = true;
e.printStackTrace();
}
reportResultAndExit();
}
protected void killServer(final int id) throws Exception
{
String configDir = System.getProperty("exampleConfigDir");
if(configDir == null)
{
throw new Exception("exampleConfigDir must be set as a system property");
}
System.out.println("Killing server " + id);
// We kill the server by creating a new file in the server dir which is checked for by the server
// We can't use Process.destroy() since this does not do a hard kill - it causes shutdown hooks
// to be called which cleanly shutdown the server
System.out.println(configDir + "/server" + id + "/KILL_ME");
File file = new File(configDir + "/server" + id + "/KILL_ME");
file.createNewFile();
// Sleep longer than the KillChecker check period
Thread.sleep(3000);
}
protected void killServer(final int id, final int serverToWaitFor) throws Exception
{
String configDir = System.getProperty("exampleConfigDir");
if(configDir == null)
{
throw new Exception("exampleConfigDir must be set as a system property");
}
System.out.println("Killing server " + id);
// We kill the server by creating a new file in the server dir which is checked for by the server
// We can't use Process.destroy() since this does not do a hard kill - it causes shutdown hooks
// to be called which cleanly shutdown the server
System.out.println(configDir + "/server" + id + "/KILL_ME");
File file = new File(configDir + "/server" + id + "/KILL_ME");
file.createNewFile();
waitForServerStart(serverToWaitFor, 20000);
}
protected void reStartServer(final int id, final long timeout) throws Exception
{
String configDir = System.getProperty("exampleConfigDir");
if(configDir == null)
{
throw new Exception("exampleConfigDir must be set as a system property");
}
System.out.println("restarting server " + id);
// We kill the server by creating a new file in the server dir which is checked for by the server
// We can't use Process.destroy() since this does not do a hard kill - it causes shutdown hooks
// to be called which cleanly shutdown the server
File file = new File(configDir + "/server" + id + "/RESTART_ME");
file.createNewFile();
waitForServerStart(id, timeout);
}
private void waitForServerStart(int id, long timeout) throws InterruptedException
{
// wait for restart
long time = System.currentTimeMillis();
while (time < System.currentTimeMillis() + timeout)
{
try
{
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("host", "localhost");
params.put("port", 5445 + id);
TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), params);
HornetQConnectionFactory cf = HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
cf.createConnection().close();
System.out.println("server restarted");
}
catch (Exception e)
{
System.out.println("awaiting server restart");
Thread.sleep(1000);
continue;
}
break;
}
}
protected InitialContext getContext(final int serverId) throws Exception
{
HornetQExample.log.info("using " + args[serverId] + " for jndi");
Properties props = new Properties();
props.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.put("java.naming.provider.url", args[serverId]);
props.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
return new InitialContext(props);
}
protected int getServer(Connection connection)
{
DelegatingSession session = (DelegatingSession) ((HornetQConnection) connection).getInitialSession();
TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
String port = (String) transportConfiguration.getParams().get("port");
return Integer.valueOf(port) - 5445;
}
protected Connection getServerConnection(int server, Connection... connections)
{
for (Connection connection : connections)
{
DelegatingSession session = (DelegatingSession) ((HornetQConnection) connection).getInitialSession();
TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
String port = (String) transportConfiguration.getParams().get("port");
if(Integer.valueOf(port) == server + 5445)
{
return connection;
}
}
return null;
}
private void reportResultAndExit()
{
if (failure)
{
System.err.println();
System.err.println("#####################");
System.err.println("### FAILURE! ###");
System.err.println("#####################");
throw new RuntimeException("failure in running example");
}
else
{
System.out.println();
System.out.println("#####################");
System.out.println("### SUCCESS! ###");
System.out.println("#####################");
}
}
}