In the third and final part of this tutorial we will crate a MDB (Message Driven Bean) that listens to our embedded ActiveMQ from Part 1.
Pre-Requirements:
- Netbeans 6.5 +
- Glassfish Setup from Tutorial Part 1
Tutorial:
- Create a new EJB AmdMDB
In the third and final part of this tutorial we will crate a MDB (Message Driven Bean) that listens to our embedded ActiveMQ from Part 1.
Pre-Requirements:
Tutorial:
This is just a POC Java SE application to show we can write to an embedded ActiveMQ queue using an external client.
Pre-Requirements:
Tutorial:
I am quite OK with the build-in JMS in Glassfish (OpenMQ), as long you dont want to access queues from external applications or you need an admin console to easily create, delete queues, send messages and browse the queues. ActiveMQ is a good alternative. Unfortunately we could not find any end2end description hot to get the couple Glassfish-ActiveMQ up and running (under Linux, Windows should be a quite similar affair).
This tutorial describes how to install and configure ActiveMQ as embedded JMS broker, and connect a MDB to it.
Pre-Requirements:
Tutorial
Originally submitted at O’Reilly
Java Message Service, Second Edition, is a thorough introduction to the standard API that supports “messaging” — the software-to-software exchange of crucial data among network computers. With this practical guide, you’ll learn how JMS can help you solve many architectura…
Old Topic, but a good starting point.
Pros: Well-written, Helpful examples, Accurate
Best Uses: Intermediate
Describe Yourself: Maker, Developer
Despite JMS being an “old” (in IT terms) piece of technology (API), you still find requirements for it in lots of projects and the basic needs of a messaging API are fully satisfied, even not updated since 2002.
This book is one of the few books covering the topic at all, and coming with code samples is the best you can get to get started with JMS. With a fair background of your application/messaging server and your favourite IDE you should be able to get the samples running.
(legalese)
Today we proceed with last weeks JMS tutorial and create a standalone java application that sends a message to a topic.
It puzzles me to have a client application that I need to start “from within”. Lets find out how an application looks like that can run by itself, connect to a JNDI server and sends a message to topic. The key information I took from this discussion (link, Thanks to Foli and TravelEntity) at forums.sun.com.
Using: Netbeans 6.5 and a local Glassfish V2
1. Lets create a standard SE java project. We call it “JMSClient”
2. Import the necessary libraries
Applicationserver JNDI Lookup
/lib/appserv-rt.jar
/lib/appserv-admin.jar
/lib/javaee.jar
/lib/j2ee.jar
Client Lib
/imq/lib/jms.jar
/imq/lib/imq.jar
/imq/lib/imqutil.jar
/lib/install/applications/jmsra/jmsra.jar
(You find them in your glassfish home directory)
3. The complete source
package jmsclient;
import java.util.Hashtable;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Main {
public static void main(String[] args) {
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
// ---- Same sample with a queue ----
// Queue queue = null;
Topic topic = null;
MessageProducer messageProducer = null;
MapMessage message = null;
Hashtable properties = new Hashtable(2);
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.appserv.naming.S1ASCtxFactory");
properties.put(Context.PROVIDER_URL, "iiop://localhost:3700");
try {
jndiContext = new InitialContext(properties);
connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory");
// ---- Same sample with a queue ----
//queue = (Queue)jndiContext.lookup("jms/Queue");
topic = (Topic)jndiContext.lookup("jms/Topic");
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// ---- Same sample with a queue ----
//messageProducer = session.createProducer(queue);
messageProducer = session.createProducer(topic);
message = session.createMapMessage();
// ---- Preparing Mapped Message ----
message.setString("lastname", "Myer");
message.setString("firstname", "Fred");
message.setString("id", "0200");
messageProducer.send(message);
connection.close();
} catch (NamingException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
}
catch (JMSException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
}
finally {
System.out.println("ENDED.");
System.exit(0);
}
}
}
Remarks:
Remark 11.12.2010:
I seriously tried to make this running using Glassfish V3. Several hours in poking around with the various libraries, it just does not work. If ever someone comes around who found the solution, let us know !
Related posts: http://stackoverflow.com/questions/4311157/glassfish-v3-x-and-remote-standalone-client and http://www.java.net/forum/topic/glassfish/glassfish/class-and-libraries-jms-client-and-gf-3x
Remark 08.11.2010:
This is a very popular blog entry ! But it is from January 2009 and refers to Glassfish V2. The below tutorial does not work with Glassfish V3. I am currently reviewing it and creating an updated walk-through with the latest version(s). Stay tuned.
Remark 21.07.2011:
To all JMS fans, please consider ActiveMQ as alternative. I documented all necessary steps here (javadude.wordpress.com/2011/07/21/glassfish-v3-1-running-embedded-activemq-for-jms-part-1)
I gave up on Glassfish JMS (at least for external apps).
Despite tons of material, downloads and websites on JAVA enterprise applications, tools and features, there are few sources only for “Let’s get started” styled tutorials. The Netbeans tutorial is often the best starting point (link).
I was reading about the background of JMS, some of the material is of a very theoretical nature and I tried to find some simple sample to get my hands dirty. A good short article on JMS in the glassfish context you can find at java.net (link)
This tutorial describes the steps to get a most simple messaging application up and running implementing a one-to-many communication using topics, also called publish-subscribe. A enterprise client creates a message put it on a topic and a message-driven bean (MDB) retrieves the message.
Using: Netbeans 6.5 with installed Glassfish V2.
1. Creating a new JAVA EE project.
2. We call it MyJMS. Disable the web-application and enable application-client
3. We will have a project window with 2 subprojects for the EJB and the application client.
4. Before we start with the coding of the EJB and the client, we configure the applicationserver. Open the admin console via right click on the application server under services (Start the server first, if not done yet). You remember the default admin password for glassfish ? “adminadmin”.You better change if your box is open to outside world.
In the admin console we create the ressources for the JMS topic.
Create a destination resource with JNDI name “jms/topic” and Resource type “javax.jms.topic” (rest default settings).
Create a connection factory with the JNDI name “jms/TopicFactory” (rest default settings, but remove username and password).