EJB3.0如何使用MDB

发布于 2024-11-07 08:34:18 字数 2901 浏览 0 评论 0原文

嘿,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦归所梦 2024-11-14 08:34:18

以下是我在正在运行的 MDB 上使用的注释的示例:

@MessageDriven(
    messageListenerInterface = javax.jms.MessageListener.class,
    name = "EventQueueListenerMDB",
    mappedName = Constants.EVENT_QUEUE_JNDI,
    activationConfig = {
            @ActivationConfigProperty(
                    propertyName = "connectionFactoryJndiName",
                    propertyValue = Constants.CONNECTION_FACTORY_JNDI),
            @ActivationConfigProperty(
                    propertyName = "destinationType",
                    propertyValue = "javax.jms.Queue")
    })

另外,您似乎拥有 javax.jms.queue 而不是 javax.jms.Queue。

Here is an example of the annotations that I have on an MDB that is working:

@MessageDriven(
    messageListenerInterface = javax.jms.MessageListener.class,
    name = "EventQueueListenerMDB",
    mappedName = Constants.EVENT_QUEUE_JNDI,
    activationConfig = {
            @ActivationConfigProperty(
                    propertyName = "connectionFactoryJndiName",
                    propertyValue = Constants.CONNECTION_FACTORY_JNDI),
            @ActivationConfigProperty(
                    propertyName = "destinationType",
                    propertyValue = "javax.jms.Queue")
    })

Also, it seems that you have javax.jms.queue not javax.jms.Queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文