Вопрос

I am using the ajax client to connect to activemq. Can the activemq ajax client support the failover transport? I use an embedded Jetty inside the broker which uses vm transport to forward messages to the broker.

The AJAX client sends messages to the broker using the URL

http://localhost:8161/api/amq

How can I bring failover support into this.

FWIW I am using amq.js.

Thanks, Bhanu

Это было полезно?

Решение

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>

Другие советы

The failover transport is for OpenWire connections. For HTTP you need some other fail over mechanism, such as a load balancer that is aware of which nodes are online or not.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top