RSS

EJB3 Message Driven Bean

18 Mar

Queue Message (Jboss AS7.1)

queue.jsp (Message Producer)

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ page import=“javax.naming.*,java.text.*,javax.jms.*,java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Queue Message</title>

</head>

<body>

<%

QueueConnection conn = null;

QueueSession session1 = null;

try {

//Set Properties of JNDI

final Properties jndiProperties = new Properties();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);

jndiProperties.put(Context.URL_PKG_PREFIXES,

“org.jboss.ejb.client.naming”);

//jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, “org.jboss.naming.remote.client.InitialContextFactory”);

Context ctx = new InitialContext(jndiProperties);

 

//QueueConnectionFactory, QueueConnection, QueueSession

QueueConnectionFactory factory = (QueueConnectionFactory) ctx

.lookup(“java:/ConnectionFactory”);

conn = factory.createQueueConnection();

session1 = conn.createQueueSession(false,

QueueSession.AUTO_ACKNOWLEDGE);

 

//QueueConnection Starts

conn.start();

//Retrieve Destination

Destination destination = (javax.jms.Queue) ctx

.lookup(“queue/test”);

out.println(“Send the following info to Message Driven Bean: <br>”);

 

//Create Message Producer

MessageProducer producer = session1.createProducer(destination);

 

//Send Text

TextMessage msg = session1

.createTextMessage(“This is my first Message Driven Bean”);

//out.println(“This is my first Message Driven Bean”);

producer.send(msg);

 

//Send Map Message

MapMessage mapmsg = session1.createMapMessage();

mapmsg.setObject(“No.1”, “Dalian”);

//out.println(“Dalian<br>”);

producer.send(mapmsg);

 

//Send BytesMessage

BytesMessage bmsg = session1.createBytesMessage();

bmsg.writeBytes(“This is BytesMessage”.getBytes());

//out.println(“This is BytesMessage”);

producer.send(bmsg);

 

//Send StreamMessage

StreamMessage smsg = session1.createStreamMessage();

smsg.writeString(“May Day”);

producer.send(smsg);

 

out.println(“All messages are sent, please check JBoss Console<br>”);

 

} catch (Exception e) {

out.println(e.getMessage());

} finally {

try {

session1.close();

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

%>

</body>

</html>

 

Messager.java (Message Consumer)

package com.xavier.ejb3.messagedrivenbean;

import java.io.ByteArrayOutputStream;

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import javax.jms.BytesMessage;

import javax.jms.MapMessage;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.StreamMessage;

import javax.jms.TextMessage;

 

@MessageDriven(name = “MessagerMDB”, activationConfig = {

@ActivationConfigProperty(propertyName = “destinationType”, propertyValue = “javax.jms.Queue”),

@ActivationConfigProperty(propertyName = “destination”, propertyValue = “queue/test”) })

public class Messager implements MessageListener {

 

public void onMessage(Message msg) {

try {

if (msg instanceof TextMessage) {

TextMessage tmsg = (TextMessage) msg;

String content = tmsg.getText();

this.print(“Text: “+ content);

} else if (msg instanceof MapMessage) {

MapMessage map = (MapMessage) msg;

String content = (String) map.getString(“No.1”);

this.print(“Map Message: “+ content);

} else if (msg instanceof BytesMessage) {

BytesMessage bmsg = (BytesMessage) msg;

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

byte[] buffer = new byte[256];

int length = 0;

while ((length = bmsg.readBytes(buffer)) != -1) {

byteStream.write(buffer, 0, length);

}

String content = new String(byteStream.toByteArray());

byteStream.close();

this.print(“BytesMessage: “+ content);

} else if (msg instanceof StreamMessage) {

StreamMessage smsg = (StreamMessage) msg;

String content =smsg.readString();

this.print(“StreamMessage: “+ content);

}

} catch (Exception e) {

e.printStackTrace();

}

}

 

private void print(String content) {

System.out.println(content);

}

 

}

 

Topic Message (Jboss AS7.1)

topic.jsp (Message Producer)

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ page import=“javax.naming.*,java.text.*,javax.jms.*,java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Topic Message</title>

</head>

<body>

<%

TopicConnection conn = null;

TopicSession session1 = null;

try {

//Set Properties of JNDI

final Properties jndiProperties = new Properties();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);

jndiProperties.put(Context.URL_PKG_PREFIXES,

“org.jboss.ejb.client.naming”);

//jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, “org.jboss.naming.remote.client.InitialContextFactory”);

Context ctx = new InitialContext(jndiProperties);

 

//TopicConnectionFactory, TopicConnection, TopicSession

TopicConnectionFactory factory = (TopicConnectionFactory) ctx

.lookup(“java:/ConnectionFactory”);

conn = factory.createTopicConnection();

session1 = conn.createTopicSession(false,

TopicSession.AUTO_ACKNOWLEDGE);

 

//TopicConnection Starts

//conn.start();

//Retrieve Destination

Destination destination = (javax.jms.Topic) ctx

.lookup(“topic/test”);

out.println(“Send the following info to Message Driven Beans: <br>”);

 

//Create Message Producer

MessageProducer producer = session1.createProducer(destination);

 

//Send Text

TextMessage msg = session1

.createTextMessage(“This is my first Message Driven Bean”);

//out.println(“This is my first Message Driven Bean”);

producer.send(msg);

 

} catch (Exception e) {

out.println(e.getMessage());

} finally {

try {

session1.close();

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

%>

</body>

</html>

 

TopicPrintBean1 (Message Consumer)

package com.xavier.ejb3.messagedrivenbean;

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

 

@MessageDriven(

activationConfig = {

@ActivationConfigProperty(propertyName = “destinationType”, propertyValue = “javax.jms.Topic”),

@ActivationConfigProperty(propertyName = “destination”, propertyValue=“topic/test”)

})

public class TopicPrintBean1 implements MessageListener {

 

    public void onMessage(Message message) {

       try{

           TextMessage tmsg = (TextMessage) message;

           String content = tmsg.getText();

           System.out.println(this.getClass().getName() + “==” + content);

       }catch(Exception e){

           e.printStackTrace();

       }

    }

 

}

 

TopicPrintBean2 (Message Consumer)

package com.xavier.ejb3.messagedrivenbean;

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

 

@MessageDriven(

activationConfig = {

@ActivationConfigProperty(propertyName = “destinationType”, propertyValue = “javax.jms.Topic”),

@ActivationConfigProperty(propertyName = “destination”, propertyValue = “topic/test”),

})

public class TopicPrintBean2 implements MessageListener {

 

    public void onMessage(Message message) {

        try{

            TextMessage tmsg = (TextMessage)message;

            String content = tmsg.getText();

            System.out.println(this.getClass().getName() + “==” + content);

        }catch(Exception e){

            e.printStackTrace();

        }

    }

 

}

Note: Life Cycle Annotation of MDB are @PostConstruct and @PreDestroy.

Advertisements
 
Leave a comment

Posted by on 03/18/2012 in EJB

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: