EJB3.0如何使用MDB
嘿,我是 j2ee 编码新手。我的问题是关于 MDB 的。 我已经设置了我的 weblogic 11g 服务器。并且队列已设置。
我已经编写了客户端代码,它是一个向队列发送消息的 java se 客户端。
import java.util.Hashtable;
import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class MyMDBClient {
/**
* @param args
*/
public static void main(String[] args) {
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;
InitialContext ctx;
Hashtable ht = new Hashtable();
try {
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
ctx = new InitialContext(ht);
Queue queue = (Queue) ctx.lookup("jms/testQueue");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
cnn = factory.createQueueConnection();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = session.createTextMessage();
msg.setText("helloworld");
sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");
session.close(); // this is important.
} catch (Exception e) {
e.printStackTrace();
}
}
}
我可以在 weblogic 管理控制台上看到队列上的消息。
但是,我在服务器端的mdb没有被调用。
谁能告诉我出了什么问题。我更喜欢使用 java 注释的解决方案。感谢大家阅读本文。
这是我的服务器代码:
/**
* import
*
*/
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import weblogic.ejbgen.*;
/**
* Message-Driven Bean implementation class for: MyMDB
*
*/
@MessageDriven(ejbName="mdb", destinationType="javax.jms.queue",destinationJndiName="jms/testQueue" )
public class mdb implements MessageListener {
/**
* Default constructor.
*/
public mdb() {
}
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
TextMessage tmsg = null;
tmsg = (TextMessage) message;
System.out.println("----------------");
System.out.println("Received message : ");
try {
System.out.println(tmsg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------------");
}
}
Hey, I am new to coding in j2ee. My question is regarding MDB.
I have already set up my weblogic 11g server. and the queue has been set up.
I have written the client code which is a java se client that sends a msg to the queue.
import java.util.Hashtable;
import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class MyMDBClient {
/**
* @param args
*/
public static void main(String[] args) {
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;
InitialContext ctx;
Hashtable ht = new Hashtable();
try {
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
ctx = new InitialContext(ht);
Queue queue = (Queue) ctx.lookup("jms/testQueue");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
cnn = factory.createQueueConnection();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = session.createTextMessage();
msg.setText("helloworld");
sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");
session.close(); // this is important.
} catch (Exception e) {
e.printStackTrace();
}
}
}
I can see the message on the queue on my administrative console of weblogic.
however, my mdb on the server end is not invoked.
Can anyone tell me what is wrong. I will prefer the solution to be in java annotations. thank you all for reading this.
here is my server code:
/**
* import
*
*/
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import weblogic.ejbgen.*;
/**
* Message-Driven Bean implementation class for: MyMDB
*
*/
@MessageDriven(ejbName="mdb", destinationType="javax.jms.queue",destinationJndiName="jms/testQueue" )
public class mdb implements MessageListener {
/**
* Default constructor.
*/
public mdb() {
}
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
TextMessage tmsg = null;
tmsg = (TextMessage) message;
System.out.println("----------------");
System.out.println("Received message : ");
try {
System.out.println(tmsg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------------");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我在正在运行的 MDB 上使用的注释的示例:
另外,您似乎拥有 javax.jms.queue 而不是 javax.jms.Queue。
Here is an example of the annotations that I have on an MDB that is working:
Also, it seems that you have javax.jms.queue not javax.jms.Queue.