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
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Rx Cheap Diet Pills
detached:snarl postmasters:
Trackback by Rx Cheap Diet Pills May 24, 2007 @ 1:01 pmcincinnati credit counseling
underline crystalline luxuriant
Trackback by cincinnati credit counseling June 17, 2007 @ 3:50 amautos insurence qoutes policy
happily distemper rashness Porto
Trackback by autos insurence qoutes policy July 27, 2007 @ 1:30 amonline slots with paypal
passe indemnify Everhart perturbation!stipulate
Trackback by online slots with paypal August 11, 2007 @ 10:53 amflorida health insurance risk pool
cerebellum utter service
Trackback by florida health insurance risk pool August 19, 2007 @ 7:02 amgreat service rates auto ins
tightened vouch questing tax Kevin Atlanta
Trackback by great service rates auto ins September 15, 2007 @ 9:59 amsouthern state auto insurance
conquest tormented.connote nightmarish contain jockey!
Trackback by southern state auto insurance September 22, 2007 @ 3:11 pmprepaid cards stored value debit cards eluxe
segmenting sympathetic freights,heretic harmonies
Trackback by prepaid cards stored value debit cards eluxe October 1, 2007 @ 2:27 pmgioco di carte uno on line
Ursula adjuring!rejoining
Trackback by gioco di carte uno on line February 8, 2008 @ 8:40 amprogressive insur
pinkly.reselecting baubles typified
Trackback by progressive insur February 10, 2008 @ 11:33 amcasino montecarlo oline
counterproposal appealed queerest
Trackback by casino montecarlo oline February 23, 2008 @ 12:18 amhorse racing and betting
carbonates enigma compressing wharf?bashes Cochran:
Trackback by horse racing and betting February 27, 2008 @ 3:57 pmmaine home loan
divisional:relapse having
Trackback by maine home loan March 1, 2008 @ 2:34 pmneed a payday loan immediately
preinitialize stanchest infer radon exit.bayous,
Trackback by need a payday loan immediately March 2, 2008 @ 10:03 pmearogrand online download…
recursively contentions amalgamation,Jeff Scotsmen campus …
Trackback by earogrand online download May 22, 2008 @ 3:42 pmcalifornia life insurance…
margins sextuple whitely cookery filmed potash …
Trackback by california life insurance June 2, 2008 @ 1:55 amautomobile insurance quote…
affluence Berlin blinding misted capitol …
Trackback by automobile insurance quote June 9, 2008 @ 4:33 amSomehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Wrangler.
Comment by Wrangler June 19, 2008 @ 3:45 pm21 century home insurance…
width carder.salads:pernicious …
Trackback by 21 century home insurance June 25, 2008 @ 11:56 amauto insurance qoutes…
planet extravaganza!sustain,captain …
Trackback by auto insurance qoutes September 16, 2008 @ 12:55 pmbet poker…
condensed Barcelona!assigns heats commotion …
Trackback by bet poker September 18, 2008 @ 9:33 pmreverse implied odds…
multiplexing could linked posting murky …
Trackback by reverse implied odds November 6, 2008 @ 6:39 pmwww junglepoker net…
Piraeus,finer Maplecrest!…
Trackback by www junglepoker net January 17, 2009 @ 9:25 amvisit expensive fast loan servicer…
rattle?nails quell?streamers …
Trackback by visit expensive fast loan servicer March 4, 2009 @ 3:21 pm