我正在尝试使用几个Java的stomp库(gozirra,stompj,activemq)。
这些库都有较差的文档,只有一个示例,并且我有一个严重的问题:
我需要SSL支持。
stomp+ssl协议已存在并由activemq支持,但我找不到支持它的Java客户端。
StompConnection connection = new StompConnection();
connection.open("localhost", 61613);
connection.connect("system", "manager");
StompFrame connect = connection.receive();
if (!connect.getAction().equals(Stomp.Responses.CONNECTED)) {
throw new Exception ("Not connected");
}
connection.begin("tx1");
connection.send("/queue/test", "message1", "tx1", null);
connection.send("/queue/test", "message2", "tx1", null);
connection.commit("tx1");
connection.subscribe("/queue/test", Subscribe.AckModeValues.CLIENT);
connection.begin("tx2");
StompFrame message = connection.receive();
System.out.println(message.getBody());
connection.ack(message, "tx2");
message = connection.receive();
System.out.println(message.getBody());
connection.ack(message, "tx2");
connection.commit("tx2");
connection.disconnect();
使用ActiveMQ库,可以这样实现:
System.setProperty("javax.net.ssl.keyStore",
"/home/foo/.keystore/client.ks");
System.setProperty("javax.net.ssl.keyStorePassword", "changeme");
System.setProperty("javax.net.ssl.trustStore",
"/home/foo/.keystore/client.ts");
StompConnection connection = new StompConnection();
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslsocket = (SSLSocket) factory.createSocket("127.0.0.1",
61612);
connection.open(sslsocket);
不确定它是否适用于Android,但可以尝试一下FuseSource StompJMS客户端,该客户端在Github上可用。它使用了hwatdispatch库,该库内置了标准的TCP和SSL传输协议。无论如何都值得一试。