Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

VÍ DỤ VỀ MESSAGE-DRIVEN BEANS ĐƠN GIẢN

VÍ DỤ VỀ MESSAGE-DRIVEN BEANS ĐƠN GIẢN

Create by: Võ văn Hải

Email: vovanhaiQN.google.com

Home page: https://vovanhai.wordpress.com

PDF version here

Thiết kế 1 ví dụ ứng dụng:

1.    Tạo thư mục MDB_Simple_EX

2.    Tạo thư mục server.

3.    Tạo file MessageBean.java trong thư mục server có nội dung sau

import javax.ejb.*;

import javax.naming.*;

import javax.jms.*;

/**

* The MessageBean class is a message-driven bean.  It implements

* the javax.ejb.MessageDrivenBean and javax.jms.MessageListener

* interfaces. It is defined as public (but not final or

* abstract).  It defines a constructor and the methods

* setMessageDrivenContext, ejbCreate, onMessage, and

* ejbRemove.

*/

public class MessageBean implements MessageDrivenBean,

MessageListener {

private transient MessageDrivenContext mdc = null;

private Context context;

/**

* Constructor, which is public and takes no arguments.

*/

public MessageBean() {

System.out.println(“In MessageBean.MessageBean()”);

}

/**

* setMessageDrivenContext method, declared as public (but

* not final or static), with a return type of void, and

* with one argument of type javax.ejb.MessageDrivenContext.

*

* @param mdc    the context to set

*/

public void setMessageDrivenContext(MessageDrivenContext mdc)

{

System.out.println(“In ” +

“MessageBean.setMessageDrivenContext()”);

this.mdc = mdc;

}

/**

* ejbCreate method, declared as public (but not final or

* static), with a return type of void, and with no

* arguments.

*/

public void ejbCreate() {

System.out.println(“In MessageBean.ejbCreate()”);

}

/**

* onMessage method, declared as public (but not final or

* static), with a return type of void, and with one argument

* of type javax.jms.Message.

*

* Casts the incoming Message to a TextMessage and displays

* the text.

*

* @param inMessage    the incoming message

*/

public void onMessage(Message inMessage) {

TextMessage msg = null;

try {

if (inMessage instanceof TextMessage) {

msg = (TextMessage) inMessage;

System.out.println(“MESSAGE BEAN: Message ” +

“received: ” + msg.getText());

} else {

System.out.println(“Message of wrong type: ” +

inMessage.getClass().getName());

}

} catch (JMSException e) {

System.err.println(“MessageBean.onMessage: ” +

“JMSException: ” + e.toString());

mdc.setRollbackOnly();

} catch (Throwable te) {

System.err.println(“MessageBean.onMessage: ” +

“Exception: ” + te.toString());

}

}

/**

* ejbRemove method, declared as public (but not final or

* static), with a return type of void, and with no

* arguments.

*/

public void ejbRemove() {

System.out.println(“In MessageBean.remove()”);

}

}

4.    Tạo thư mục META-INF

5.    Tạo tập tin ejb-jar.xml trong thư mục META-INF có nội dung sau

<!DOCTYPE ejb-jar PUBLIC

“-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN”

http://java.sun.com/dtd/ejb-jar_2_0.dtd”&gt;

<ejb-jar>

<enterprise-beans>

<message-driven>

<ejb-name>TestMDB</ejb-name>

<ejb-class>MessageBean</ejb-class>

<transaction-type>Container</transaction-type>

<message-driven-destination>

<destination-type>javax.jms.Queue</destination-type>

<subscription-durability>NonDurable</subscription-durability>

</message-driven-destination>

</message-driven>

</enterprise-beans>

</ejb-jar>

6.    Tạo tập tin jboss.xml trong thư mục META-INF có nội dung sau

<?xml version=”1.0″ encoding=”UTF-8″?>

<jboss>

<enterprise-beans>

<message-driven>

<ejb-name>TestMDB</ejb-name>

<destination-jndi-name>queue/TestMDB</destination-jndi-name>

</message-driven>

</enterprise-beans>

</jboss>

7.    Tạo file compile.bat có nội dung sau

javac -d . *.java

pause

Chạy file này để biên dịch.

8.    Tạo tập tin createJAR.bat có nội dung sau

jar cvf TestMDB.jar *.class META-INF/*.xml

pause

Chạy file này ta sẽ có tập tin TestMDB.jar.

9.    Triển khai ứng dụng bằng cách copy vào thư mục %JBOSS_HOME%\server\default\deploy, ta được kết quả như sau nếu thành công

19:51:41,620 INFO  [EjbModule] Deploying TestMDB

19:51:41,729 WARN  [JMSContainerInvoker] Could not find the queue destination-jn

di-name=queue/TestMDB

19:51:41,729 WARN  [JMSContainerInvoker] destination not found: queue/TestMDB reason: javax.naming.NameNotFoundException: TestMDB not bound

19:51:41,729 WARN  [JMSContainerInvoker] creating a new temporary destination: q

ueue/TestMDB

19:51:41,745 INFO  [TestMDB] Bound to JNDI name: queue/TestMDB

19:51:41,776 INFO  [EJBDeployer] Deployed: file:/C:/javaSoft/jboss-4.2.2.GA/serv

er/default/deploy/TestMDB.jar

10.  Tạo thư mục client

11.  Tạo file SimpleClient.java trong thư mục client với nội dung sau

import javax.jms.*;

import javax.naming.*;

/**

* The SimpleClient class sends several messages to a queue.

*/

public class SimpleClient {

/**

* Main method.

*/

public static void main(String[] args) {

System.setProperty(“java.naming.factory.initial”,”org.jnp.interfaces.NamingContextFactory”);

System.setProperty(“java.naming.provider.url”,”localhost:1099″);

Context                 jndiContext = null;

QueueConnectionFactory  queueConnectionFactory = null;

QueueConnection         queueConnection = null;

QueueSession            queueSession = null;

Queue                   queue = null;

QueueSender             queueSender = null;

TextMessage             message = null;

final int               NUM_MSGS = 10;

/*

* Create a JNDI API InitialContext object.

*/

try {

jndiContext = new InitialContext();

} catch (NamingException e) {

System.out.println(“Could not create JNDI API ” +

“context: ” + e.toString());

System.exit(1);

}

/*

* Look up connection factory and queue.  If either does

* not exist, exit.

*/

try {

queueConnectionFactory = (QueueConnectionFactory)

jndiContext.lookup(“ConnectionFactory”);

queue = (Queue)

jndiContext.lookup(“queue/TestMDB”);

} catch (NamingException e) {

System.out.println(“JNDI API lookup failed: ” +

e.toString());

System.exit(1);

}

/*

* Create connection.

* Create session from connection; false means session is

* not transacted.

* Create sender and text message.

* Send messages, varying text slightly.

* Finally, close connection.

*/

try {

queueConnection =

queueConnectionFactory.createQueueConnection();

queueSession =

queueConnection.createQueueSession(false,

Session.AUTO_ACKNOWLEDGE);

queueSender = queueSession.createSender(queue);

message = queueSession.createTextMessage();

for (int i = 0; i < NUM_MSGS; i++) {

message.setText(“This is message ” + (i + 1));

System.out.println(“Sending message: ” +

message.getText());

queueSender.send(message);

}

} catch (JMSException e) {

System.out.println(“Exception occurred: ” +

e.toString());

} finally {

if (queueConnection != null) {

try {

queueConnection.close();

} catch (JMSException e) {}

}

System.exit(0);

}

}

}

12.  Tạo file compile.bat với nội dung sau

javac -d . *.java

pause

Chạy file này để biên dịch

13.  Tạo file run.bat với nội dung sau

java Client

pause

14.  Thực thi file run.bat, kết quả dạng như sau:

D:\Bai giang Aptech\Aptech – Semester 4\JMS\exmples\Message-Driven Beans\mdb_JMS

\client>java SimpleClient

Sending message: This is message 1

Sending message: This is message 2

Sending message: This is message 3

D:\Bai giang Aptech\Aptech – Semester 4\JMS\exmples\Message-Driven Beans\mdb_JMS

\client>pause

Press any key to continue . . .

Lúc đó trên cửa sổ JBOSS ta nhận được kết quả sau

19:56:56,693 INFO  [STDOUT] In MessageBean.MessageBean()

19:56:56,693 INFO  [STDOUT] In MessageBean.setMessageDrivenContext()

19:56:56,693 INFO  [STDOUT] In MessageBean.ejbCreate()

19:56:56,693 INFO  [STDOUT] MESSAGE BEAN: Message received: This is message 1

19:56:56,724 INFO  [STDOUT] MESSAGE BEAN: Message received: This is message 2

19:56:56,724 INFO  [STDOUT] MESSAGE BEAN: Message received: This is message 3

CHÚC THÀNH CÔNG!

One Response to “VÍ DỤ VỀ MESSAGE-DRIVEN BEANS ĐƠN GIẢN”

  1. viet said

    thay cho em hoi muon co file jar cua flash va javax.media thi dowload trang nao

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: