WebLogicJMS
Using WebLogic JMS from NetBeans
Contents |
JMS Queue Creation Sample
In order to create JMS Queue we need to access admin console. Project specific JMS resources are not yet supported in the IDE.
WebLogic Server Registration
The first thing we need is to register and start server. We are using WebLogic 10.3.5 in our sample.
To register the server we select Services tab and right-click the Server node. From context menu we choose Add Server. In the wizard we select Oracle WebLogic Server.
In next step we have to select our installation.
And in the last step we specify a domain, username and password.
After successful registration you should see a new WebLogic node. Now you can right click the WebLogic node and choose Start item. You should see the WebLogic log in the output window.
JMS Queue Configuration
In order to make things really simple for our demonstration we use single server domain and we do not specify any persistent store for the queue.
To open admin console right-click the WebLogic node and click View Admin Console item. In the browser window fill in username and password and log in to admin console.
First we need to create a JMS Server. In Domain Structure view click on Services, Messaging and JMS Servers and click New button in the table.
Enter the server name you like and click next.
Select the admin server as target and click finish. You should see the new JMS server as presented on the next picture.
Next we need to create JMS module. In Domain Structure view click on Services, Messaging and JMS Modules and click New button in the table. Enter the module name and click next.
In the next step select the admin server as target and click next.
As we want to create JMS Queue in the module mark the checkbox asking us about that and click finish.
Now the empty JMS module is created and we have to click on New button in Resources table to add a queue.
Select the Queue resource as shown below and click next.
Enter the name of the queue and its JNDI name and click next again.
In order to properly configure a target for the JMS queue we click on "Create a New Subdeployment" button.
Enter the subdeployment name and click OK.
Finally choose the target - the JMS server we created before and finish the wizard.
We have just created simple JMS queue.
Message-driven EJB
With all the things preconfigured we can create a message driven bean using the queue. First we have to create the EJB project targeting WebLogic server.
In order to do that select File in main menu than New Project and in Java EE category select EJB module and click next.
Specify the name of the project and click next again.
In the last step select the server we registered in the beginning and finish the wizard.
To create a MDB right click the created project node and select New -> Message-Driven Bean from the context menu. In the MDB wizard fill in the bean name and select the the message queue we have created in admin console.
When finished the IDE will generate following class for you.
@MessageDriven(mappedName = "jms/Queue-0", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyMessageBean implements MessageListener {
public MyMessageBean() {
}
@Override
public void onMessage(Message message) {
}
}
We will only change the onMessage() method slightly to give us some simple feedback when message arrives.
@MessageDriven(mappedName = "jms/Queue-0", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyMessageBean implements MessageListener {
public MyMessageBean() {
}
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
System.out.println(((TextMessage) message).getText());
} else {
System.out.println(message.getJMSMessageID());
}
} catch (JMSException ex) {
Logger.getLogger(NewMessageBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Now we can run our EJB module on WebLogic server by hitting the F6 key.
Web Application
To test the MDB and the JMS queue actually works we will create a simple web application with one servlet producing the messages. We have to create the Web Application project first. To do that click on File in main menu and select New Project. In the wizard choose Java Web category and Web Application project and click next.
Configure the name and location of the project and continue to the next step.
In the last step select the WebLogic server we have registered and finish the wizard.
To create a simple servlet right click the created project node and select New -> Servlet from the context menu. In the Servlet wizard fill in the servlet name and proceed to the next step.
Here you can configure the URL for the servlet. It is safe to use the defaults.
After finishing the wizard the IDE will generate the following servlet code for us.
public class MyServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet MyServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
...
}
We will reuse it to generate sample Hello messages. We use Java EE 5 annotations to inject default connection factory and our JMS queue. On every request we create a message and we send it to the queue.
public class MyServlet extends HttpServlet {
@Resource(mappedName="weblogic.jms.ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName="jms/Queue-0")
private Queue queue;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Connection connection = connectionFactory.createConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
try {
MessageProducer messageProducer = session.createProducer(queue);
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>"); out.println("</body>");
Message message = session.createTextMessage("Hello");
messageProducer.send(message);
out.println("Sent message Hello");
out.println("</html>");
} finally {
session.close();
}
} finally {
connection.close();
}
} catch (JMSException ex) {
throw new ServletException(ex);
} finally {
out.close();
}
}
...
}
Now we are ready to run the web application on our WebLogic server by hitting the F6 key.
Sending Messages
By default when you run the web application it opens the index.jsp. We need to navigate to our servlet to send a JMS message. The URL of the servlet used in our sample is http://localhost:7001/MyWebApplication/MyServlet.
By invoking the servlet the message in via the queue delivered to our MDB and the content of the message in printed to server log as shown below.




























