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.
Filed under: Uncategorized
After a great career, Michael Schumacher decides to call it quits. He announced his retirement after winning the Monza Grand Prix.
“The Scuderia also announced Kimi Raikkonen would team up with Felipe Massa in 2007″.
Full news here…
Kimi and Ferrari will definitely make history!
We had nothing to do last Saturday night. Here’s another experiment using Nikon CLS…The Canon 350D is not really for sale. I just made the comment up for fun. I used my friend’s camera as one of my subjects. This site seems to have a weird crop. To see the correct crops, visit my flickr site at http://www.flickr.com/photos/melvindave/.




