Jboss Messaging JMS [英] Jboss Messaging JMS

查看:105
本文介绍了Jboss Messaging JMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功设法将消息发送到我的localhost Jboss服务器上的队列名 ReceiverQueue ,如何检索我发送给它的消息或如何检查是否有消息队列中的任何消息(如果有)检索它们。或者我可以得到某种解释是什么是最好的方法。谢谢

I successfully managed to send the message to queue name ReceiverQueue on my localhost Jboss server, how can I retrieve message I sent to it or how do I check if there is any messages in the queue if any retrieve them. or can I get an explanation of some sort what is the best way to do this. Thank you

也可以接受有效的发送/接收教程。任何能让我发送到队列并从该队列接收消息的东西都会得到接受的答案。

A working send/receive tutorial would be accept as well. Anything that will get me to just send to the queue and receive message from that queue will get accepted answer.

我正在使用Spring。

I'm using Spring.

我想要一个使用应用程序上下文和bean注入的解决方案。

I want a solution that does it using application context with bean injection ..

推荐答案

标准JMS API步骤:

1.使用服务器的访问详细信息创建一个javax.naming.Context

Standard JMS API steps:
1. Create a javax.naming.Context with the access details of the server

context = new InitialContext(environment)

2。在上下文中查找javax.jms.QueueConnectionFactory。工厂名称特定于JMS服务器

2. Look up javax.jms.QueueConnectionFactory in the context. Factory name is specific to the JMS server

factory = (QueueConnectionFactory)context.lookup(factoryName)

3。创建一个javax.jms.QueueConnection

3. Create a javax.jms.QueueConnection

connection = factory.createQueueConnection(...)

4。创建一个javax.jms.QueueSession

4. Create a javax.jms.QueueSession

session = connection.createQueueSession(...)

5。在上下文中查找你的javax.jms.Queue

5. Look up your javax.jms.Queue in the context

queue = (Queue) context.lookup(qJndiName)

直到现在它与发送相同....

6.创建一个javax带有会话的.jms.QueueReceiver

Till now it is the same as sending....
6. Create a javax.jms.QueueReceiver with the session

receiver = session.createReceiver(queue)

7。 JMS API提供了两种检索消息的方法:
$
7.a等待带有 receiver.receive()方法之一的消息

7.b在你的类中实现javax.jms.MessageListener并将其注册为监听器

7. JMS API provides 2 ways to retrieve a message:
7.a Wait for a message with one of the receiver.receive() methods
7.b Implement javax.jms.MessageListener in your class and register it as the listener

receiver.setMessageListener(this)

JMS API将调用你的 onMessage()方法

8.不要忘记启动监听器:

JMS API will call your onMessage() method whenever a new message arrives
8. Don't forget to start the listener:

connection.start()

9。关闭上下文(非常重要,当您从同一程序访问多个JMS服务器时):

9. Close the context (very important, when you access multiple JMS servers from the same program):

context.close()

以上是独立应用程序的典型解决方案。在EJB环境中,您应该使用消息驱动的bean。您可以在 http://java.sun上找到它们。 .com / javaee / 6 / docs / tutorial / doc / gipko.html http://schuchert.wikispaces.com/EJB3+Tutorial+5+-+Message+Driven+Beans

The above is a typical solution from a stand-alone application. In EJB environment you should use message driven beans. You can find ino on them on http://java.sun.com/javaee/6/docs/tutorial/doc/gipko.html and a tutorial on http://schuchert.wikispaces.com/EJB3+Tutorial+5+-+Message+Driven+Beans

以下是您要求的工作示例:

Here is the working example you've asked for:

import java.util.Hashtable;
import javax.naming.*;
import javax.jms.*;

public class JMSJNDISample implements MessageListener {

    public static final String JNDI_URL = "jnp://localhost:1099";
    public static final String JNDI_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
    public static final String JMS_USER = null;
    public static final String JMS_PASSWORD = null;
    public static final String JMS_CONNECTION_FACTORY = "MyConnectionFactory";
    public static final String QUEUE_JNDI_NAME = "ReceiverQueue";

    QueueConnection qConn = null;
    QueueSession qSession = null;
    QueueSender qSender = null;
    QueueReceiver qReceiver = null;

    public JMSJNDISample () {
    }


    public void init() throws JMSException, NamingException {
        // Set up JNDI Context
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, JNDI_URL);
        if (JMS_USER != null)
            env.put(Context.SECURITY_PRINCIPAL, JMS_USER);
        if (JMS_PASSWORD != null)
            env.put(Context.SECURITY_CREDENTIALS, JMS_PASSWORD);
        Context jndiContext = new InitialContext(env);

        // Lookup queue connection factory
        QueueConnectionFactory cFactory = (QueueConnectionFactory)jndiContext.lookup(JMS_CONNECTION_FACTORY);

        // Create Connection
        if (JMS_USER == null || JMS_PASSWORD == null)
            qConn = cFactory.createQueueConnection();
        else {
            qConn = cFactory.createQueueConnection(JMS_USER, JMS_PASSWORD);
        }

        // Create Session
        qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        // Lookup Queue
        Queue queue = (Queue) jndiContext.lookup(QUEUE_JNDI_NAME);

        // Create Queue Sender
        qSender = qSession.createSender(queue);

        // Create Queue Receiver
        qReceiver = qSession.createReceiver(queue);
        qReceiver.setMessageListener(this);

        // Start receiving messages
        qConn.start();

        // Close JNDI context
        jndiContext.close();
    }


    public void sendMessage (String str) throws JMSException {
        TextMessage msg = qSession.createTextMessage(str);
        qSender.send(msg);
    }


    public void onMessage (Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage)message;
                System.out.println("Text Message Received: "+textMessage.getText());
            } else {
                System.out.println(message.getJMSType()+" Message Received");
            }
        } catch (JMSException je) {
            je.printStackTrace();
        }
    }


    public void destroy() throws JMSException {
        if (qSender != null) qSender.close();
        if (qReceiver != null) qReceiver.close();
        if (qSession != null) qSession.close();
        if (qConn != null) qConn.close();
    }


    public static void main(String args[]) {
        try {
            JMSJNDISample sample = new JMSJNDISample();
            // Initialize connetion
            sample.init();
            // Send Message
            sample.sendMessage("Hello World");
            // Wait 2 sec for answer
            Thread.sleep(2000);
            // Disconnect
            sample.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这篇关于Jboss Messaging JMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆