Create Message Driven Bean for Topic destination and publish message from web application

Here I am creating Enterprise Application with EJB and Web Module. In the EJB module create Message Driven Bean (MDB) for Topic destination. And send publish message from Web Application. The published messages are visible on Server clonsole.

Below are steps to create Message Driven Bean for Topic on Server :

  • Install Oracle WebLogic Server 10.3.2
  • Create Domain on Orcale WebLogic Server 10.3.2
  • Configure Domain in Eclipse
  • Create administrator objects for Topic on Server
  • Create Enterprise Application Project "EJB3MDBTopic.ear". with EJB and Web Module
  • Create Message Driven Bean for Topic
  • Create user interface and Servlet to send message
  • Run application and send message

Step 1: Install Oracle WebLogic Server 10.3.2 [ Click here - How to install WebLogic Server ]

Step 2: Create Domain on Orcale WebLogic Server 10.3.2 [ Click here - How to create Domain on WebLogic Server ]

Step 3: Configure Domain in Eclipse [ Click here - How to Configure Domain in Eclipse ]

Step 4: Create administrator objects for Topic on Server [ Click here - How to create JMS administrator objects on Server]

Step 5: Create Enterprise Application Project with EJB and Web Module
File -> New -> Enterprise Appliction Project
Create Enterprise Project
Specify the Enterprise Project name EJB3MDBTopic and click Next.

Below screen is used to create EJB and Web module in Enterprise Project. Click New Module


Check EJB Module and Web Module and Click Finish.


You will get below screen and Click Finish

This step will create Enterprise Project with EJB and Web project.

Step 6: Create Message Driven Bean
Create MDB : MessageTopic1.java
package techmyguru.topic;

// all imports
@MessageDriven(activationConfig = @ActivationConfigProperty
(propertyName = "destinationType", propertyValue = 
"javax.jms.Topic") }, mappedName = "tjndi1")
public class MessageTopic1 implements MessageListener 
{
@Resource
private MessageDrivenContext mdc;
public void onMessage(Message inMessage
{
TextMessage msg = null;
try {
if (inMessage instanceof TextMessage
{
  msg = (TextMessageinMessage;
  String message = msg.getText();
  RandomAccessFile file = new RandomAccessFile
  ("MyMessageTopic.txt""rw");
  file.seek(file.length());
  file.writeBytes(message + " ");
  file.close();
  System.out.println("Topic - Message received: " + message);

else 
{
System.out.println("Message of wrong type: "+ inMessage.getClass()
.getName());
}

catch (JMSException e
{
  e.printStackTrace();
  mdc.setRollbackOnly();
catch (Throwable te) {
  te.printStackTrace();
}
}
}

Step 7: Create user interface and Servlet to send message
User interface : index.jsp


MDB Topic example


Topic Demo


"TopicPublisherServlet">
Enter your message "text" name="msg">
"submit" value="Send Message">



Servlet : TopicPublisherServlet.java
package techmyguru.web;

// all imports
public class TopicPublisherServlet extends HttpServlet {

@Resource(mappedName = "cfjndi")
private TopicConnectionFactory topicconnectionfactory;

@Resource(mappedName = "tjndi1")
private Topic topic;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TopicConnection topicconnection=null;
TopicSession topicsession=null;
PrintWriter pw = response.getWriter();
pw.println("Message seding from client
"
);
try {
topicconnection=topicconnectionfactory.createTopicConnection();
topicconnection.start();
topicsession=topicconnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
TopicPublisher publisher=topicsession.createPublisher(topic);
TextMessage txtMsg=topicsession.createTextMessage();
txtMsg.setText(request.getParameter("msg"));
publisher.publish(txtMsg);
pw.println("To see message go to server console
"

    "Try Again");

catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
finally {
if (topicconnection != null) {
  try {
  topicconnection.close();
  catch (JMSException e) {
  }
}
}
}
}

Step 8: Run application and send message
Verify the project directory structure :


Run Enterprise application and get output :


Type message and click Send Message button and see server console.

Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code