Using ActiveMQ with Java Client

Using ActiveMQ with Java Client

There is 3 kinds of client we can consider.
1. It is from ActiveMQ, I think there is a build-in client in activemq.
2. It is named Gozirra, I do not know it is kept updating.
3. It is named stompj, I prefer to use this. I can see the comments on Feb. 9 2011.

my ivy.xml configuration file:
<!--  stompj client -->
<dependency org="pk/aamir" name="stompj" rev="0.5" />

My test class TestMain.java:

package com.sillycat.easyai;
import pk.aamir.stompj.Connection;
import pk.aamir.stompj.DefaultMessage;
import pk.aamir.stompj.ErrorHandler;
import pk.aamir.stompj.ErrorMessage;
import pk.aamir.stompj.Message;
import pk.aamir.stompj.MessageHandler;
import pk.aamir.stompj.StompJException;
public class TestMain extends Thread{
private Connection con = null;
public void run(){
// Connection con = new Connection("localhost", 61613, "userid",
// "password");
con = new Connection("localhost", 61613);
try {
con.connect();
con.send("<username>sillycat</username><userage>13</userage>",
"/queue/foo");
DefaultMessage msg = new DefaultMessage();
msg.setProperty("type", "text/plain");
msg.setContent("Another test message!");
con.send(msg, "/queue/foo");
con.subscribe("/queue/foo", true);
con.addMessageHandler("/queue/foo", new MessageHandler() {
public void onMessage(Message msg) {
System.out.println(msg.getContentAsString());
}
});
con.setErrorHandler(new ErrorHandler() {
public void onError(ErrorMessage errorMsg) {
System.out.println(errorMsg.getMessage());
System.out.println(errorMsg.getContentAsString());
}
});
} catch (StompJException e) {
e.printStackTrace();
} finally {
//con.disconnect();
}
}
public static void main(String[] args) {
TestMain testMain = new TestMain();
testMain.start();
}
}

This is just a very simple sample. It is not good implementation. But from this sample, I prooved that I can make perl and
java communicate well via Queue.
One puts the message in queue, one just consumes it.

references:
http://activemq.apache.org/stomp.html
http://www.germane-software.com/software/Java/Gozirra/
http://code.google.com/p/stompj/wiki/GettingStarted

你可能感兴趣的:(java,apache,activemq,Google,perl)