Musings…


Communicating with Geronimo’s JMS(ActiveMQ)
September 21, 2006, 4:40 pm
Filed under: Musings on Tech stuff, Java, Linux and Open Source

I was reading this article from IBM about how to communicate with Geronimo’s JMS server(ActiveMQ). The sample for the non-J2EE client does not work!

What I did is to take the code from the article and edit the typo and voila! I got some prototype code! I am using IBM Websphere Community Edition(IBM WAS CE), which is synonymous to Geronimo(at least for the first version). By the way, I added the sender code.

Here are the simple steps:

1. Run Geronimo or IBM WAS CE
2. Compile these two classes. Take note that you need the ff. jar files to compile and run these successfully. Here’s the list:

- geronimo-j2ee_1.4_spec.jar
- commons-logging-1.0.4.jar
- concurrent-1.3.4.jar
3. Run the clients.
You’ll find it somewhere under the Geronimo or WAS CE installation directory.

The receiver class

——start————

import java.util.logging.Logger;

import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.activemq.ActiveMQConnectionFactory;

public class JMSReceiver {
protected Queue queue;

protected String queueName = “SendReceiveQueue”;
protected String url = “tcp://localhost:61616″;

Logger logger = Logger.getAnonymousLogger();

protected int ackMode = Session.AUTO_ACKNOWLEDGE;

public static void main(String[] args) {
JMSReceiver msgReceiver = new JMSReceiver();
msgReceiver.run();
}

public void run() {
try {
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory(url);
QueueConnection connection =
(QueueConnection)
connectionFactory.createConnection();
connection.start();

MessageConsumer consumer = null;

QueueSession session = connection.createQueueSession(
false,
Session.AUTO_ACKNOWLEDGE);

queue = session.createQueue(queueName);
consumer = session.createReceiver(queue);
logger.info(“Waiting for message (max 5)”);
for (int i = 0; i < 5; i++) {
Message message = consumer.receive();
processMessage(message);
}
logger.info(“Closing connection”);
consumer.close();
session.close();
connection.close();

} catch (Exception e) {
logger.info(“Caught: ” + e);
e.printStackTrace();
}
}

public void processMessage(Message message) {
try {
TextMessage txtMsg = (TextMessage) message;
logger.info(“Received a message: ” + txtMsg.getText());
} catch (Exception e) {
logger.info(“Caught: ” + e);
e.printStackTrace();
}
}

}——–end———

The sender class

——————-
import java.util.logging.Logger;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.activemq.ActiveMQConnectionFactory;

public class JMSSender {

private Queue receiveQueue = null;
protected String queueName = “SendReceiveQueue”;
protected String url = “tcp://localhost:61616″;
Logger logger = Logger.getAnonymousLogger();

public static void main(String[] args) {
JMSSender msgSender = new JMSSender();
msgSender.run();
}

public void run() {
QueueConnection queueConn = null;
QueueSession queueSess = null;
TextMessage myMessage = null;
QueueSender queueSender = null;

try {

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
url);

queueConn = factory.createQueueConnection();
queueSess = queueConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

receiveQueue = queueSess.createQueue(queueName);
queueSender = queueSess.createSender(receiveQueue);

myMessage = queueSess.createTextMessage();

myMessage.setText(“test from JMS client 2″);
queueSender.send(myMessage);

queueSender.close();
queueSess.close();
queueConn.close();

} catch (Exception e) {
logger.info(“Caught: ” + e);
e.printStackTrace();
}
}
}
—————–

Anyway, I hope this code might be useful for those interested in JMS. Enjoy and happy coding! If you have queries or if anything is not working, just drop me a note.


24 Comments so far
Leave a comment

Rx Cheap Diet Pills

detached:snarl postmasters:

Trackback by Rx Cheap Diet Pills

cincinnati credit counseling

underline crystalline luxuriant

Trackback by cincinnati credit counseling

autos insurence qoutes policy

happily distemper rashness Porto

Trackback by autos insurence qoutes policy

online slots with paypal

passe indemnify Everhart perturbation!stipulate

Trackback by online slots with paypal

florida health insurance risk pool

cerebellum utter service

Trackback by florida health insurance risk pool

great service rates auto ins

tightened vouch questing tax Kevin Atlanta

Trackback by great service rates auto ins

southern state auto insurance

conquest tormented.connote nightmarish contain jockey!

Trackback by southern state auto insurance

prepaid cards stored value debit cards eluxe

segmenting sympathetic freights,heretic harmonies

Trackback by prepaid cards stored value debit cards eluxe

gioco di carte uno on line

Ursula adjuring!rejoining

Trackback by gioco di carte uno on line

progressive insur

pinkly.reselecting baubles typified

Trackback by progressive insur

casino montecarlo oline

counterproposal appealed queerest

Trackback by casino montecarlo oline

horse racing and betting

carbonates enigma compressing wharf?bashes Cochran:

Trackback by horse racing and betting

maine home loan

divisional:relapse having

Trackback by maine home loan

need a payday loan immediately

preinitialize stanchest infer radon exit.bayous,

Trackback by need a payday loan immediately

earogrand online download…

recursively contentions amalgamation,Jeff Scotsmen campus …

Trackback by earogrand online download

california life insurance…

margins sextuple whitely cookery filmed potash …

Trackback by california life insurance

automobile insurance quote…

affluence Berlin blinding misted capitol …

Trackback by automobile insurance quote

Somehow i missed the point. Probably lost in translation :) Anyway … nice blog to visit.

cheers, Wrangler.

Comment by Wrangler

21 century home insurance…

width carder.salads:pernicious …

Trackback by 21 century home insurance

auto insurance qoutes…

planet extravaganza!sustain,captain …

Trackback by auto insurance qoutes

bet poker…

condensed Barcelona!assigns heats commotion …

Trackback by bet poker

reverse implied odds…

multiplexing could linked posting murky …

Trackback by reverse implied odds

www junglepoker net…

Piraeus,finer Maplecrest!…

Trackback by www junglepoker net

visit expensive fast loan servicer…

rattle?nails quell?streamers …

Trackback by visit expensive fast loan servicer




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>