消息侦听器onMessage()方法不会使队列中的消息变红

时间:2017-02-10 07:46:58

标签: java jms

我正在实现MessageListner类的onMessgae()方法,但它不起作用,即使我将调试点放在此方法中,但调试器也没有进入此方法。

使用此方法从队列接收消息。 下面是我接收邮件的代码,但是当我通过发送者类发送邮件时它没有给出任何消息,而且它的消息转到JMS服务器并在服务器上以这种方式显示

名称服务器DestiCurrent MessagesCurrent MessagesPending MessagesReceived JMSServer-0 AdminServer 3 15 1 2

Reciver代码:

{
import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class QueueReceive implements MessageListener {
    public final static String Server="t3://localhost:7001";
    public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    public final static String JMS_FACTORY = "CF1";
    enter code here

    public final static String QUEUE = "Queue1";
    private QueueConnectionFactory qconFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private QueueReceiver qreceiver;
    private Queue queue;
    private boolean quit = false;
    String s1 = "avanish";


    public void init(Context ctx, String queueName) throws NamingException, JMSException {
        qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
        qcon = qconFactory.createQueueConnection();
        qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queue = (Queue) ctx.lookup(queueName);
        qreceiver = qsession.createReceiver(queue);
        qreceiver.setMessageListener(this);
        qcon.start();
        System.out.println(qreceiver.receive());
    }

    public void close() throws JMSException {
        qreceiver.close();
        qsession.close();
        qcon.close();
    }

    public static void main(String[] args) throws Exception {
        /*if (args.length != 1) {
            System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL");
            return;
        }*/
        InitialContext ic = getInitialContext();
        QueueReceive qr = new QueueReceive();
        qr.init(ic, QUEUE);
        System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
        synchronized (qr) {
            while (!qr.quit) {
                try {
                    qr.wait();
                } catch (InterruptedException ie) {
                }
            }
        }
        qr.close();
    }


    private static InitialContext getInitialContext() throws NamingException {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, Server);
        return new InitialContext(env);
    }

    @Override
    public void onMessage(Message msg) {
        try {
            String msgText;
            if(msg instanceof TextMessage){
                    msgText = ((TextMessage) msg).getText();
                } 
             else{
                 msgText = msg.toString();
            }
                 System.out.println("reciver msg" + msgText);

                 if(msgText.equalsIgnoreCase("quit")){
                     synchronized (this) {
                        quit= true;
                        this.notifyAll();
                    }
                 }
            } catch (JMSException e) {
                System.err.println("Exception" + e.getMessage());
            }

        }


}

}

发件人代码: {

    import javax.jms.JMSException;
import javax.jms.Queue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import javax.jms.*;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSend {
    public final static String Server="t3://localhost:7001";
    public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    public final static String JMS_FACTORY = "CF1";
    public final static String QUEUE = "Queue1";
//  public final static String QUEUE = "DQ";
    private QueueConnectionFactory qconFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private QueueSender qsender;
    private Queue queue;
    private TextMessage msg;

    public void init(Context ctx, String queueName) throws NamingException, JMSException

    {

        qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);

        qcon = qconFactory.createQueueConnection();

        qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        queue = (Queue) ctx.lookup(queueName);

        qsender = qsession.createSender(queue);

        msg = qsession.createTextMessage();

        qcon.start();

    }

    public void send(String message) throws JMSException {

        msg.setText(message);
        //msg.acknowledge();
        qsender.setPriority(9);
        qsender.send(msg);

    }

    public void close() throws JMSException {

        qsender.close();

        qsession.close();

        qcon.close();

    }

    public static void main(String[] args) throws Exception {


        InitialContext ic = getInitialContext();

        QueueSend qs = new QueueSend();

        qs.init(ic, QUEUE);

        readAndSend(qs);

        qs.close();

    }

    private static void readAndSend(QueueSend qs) throws IOException, JMSException

    {

        BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));

        String line = null;

        boolean quitNow = false;

        do {

            System.out.print("Enter message (\"quit\" to quit): \n");

            line = msgStream.readLine();

            if (line != null && line.trim().length() != 0) {

                qs.send(line);

                System.out.println("JMS Message Sent: " + line + "\n");

                quitNow = line.equalsIgnoreCase("quit");

            }

        } while (!quitNow);

    }

    private static InitialContext getInitialContext()

            throws NamingException

    {

        Hashtable<String, String> env = new Hashtable<String, String>();

        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

        env.put(Context.PROVIDER_URL, Server);

        return new InitialContext(env);

    }

}



}

1 个答案:

答案 0 :(得分:0)

使用MessageListener,我使用下面的代码行并从队列中获取确切的消息

    queue = (Queue) ctx.lookup(queueName);
    qreceiver = qsession.createReceiver(queue);

   message = qreceiver.receiveNoWait(); 

它成功收到了来自Queue的消息。