Add the following getting and setter functions to the end of the object returned in the amq.js file (i.e. after the testPollHandler function):
setUri: function(newUri) {
uri = newUri;
},
getUri: function() {
return uri;
}
Then do something like this to switch to the next server if a connection could not be established:
var ACTIVEMQ_SERVERS = ["http://server1:8161/ajax/amq","http://server2:8161/ajax/amq"];
var amq = org.activemq.Amq;
function connectStatusHandler(connected) {
if (!connected) {
var index = _.indexOf(ACTIVEMQ_SERVERS, amq.getUri());
var newIndex = (index + 1) % ACTIVEMQ_SERVERS.length;
var uri = ACTIVEMQ_SERVERS[newIndex];
amq.setUri(uri);
if (window.console) {
console.log("Lost connection. Attempting next server: " + uri);
}
}
}
amq.init({
uri: ACTIVEMQ_SERVERS[0],
logging: true,
timeout: 20,
connectStatusHandler: connectStatusHandler,
logging: true
});
You'll also need CORS enabled in the Jetty server that hosts the AjaxServlet if you are to establish connections across servers. To do that, enable the built in Jetty CORS filter with the following configuration in the web.xml file:
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/amq/*</url-pattern>
</filter-mapping>