为什么我的订阅者无法在jms上阅读发布者的消息

时间:2012-11-27 12:29:19

标签: java jms message publish-subscribe

我刚开始与JMS合作 所以我拿了一个 example ,它就像一个魅力! 当我在控制台中输入消息时订阅者收到了消息,但该示例仅基于文件,因此我决定将其拆分为发布者文件和订阅者文件 但它不会工作,订阅者不会阅读该消息! 我从this other example插入了一些代码,但仍然没有,我无法理解为什么我的程序完美编译而没有错误,有人知道它为什么不读取消息吗?

我的Subscriver.java文件是:

package main;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sun.messaging.Destination;
public class Subscriver1 {
private TopicSession subSession;
private TopicSubscriber subscriver;
private TopicConnection connection;
private String username;
Destination dest;

public Subscriver1(String topicName, String username, String password)
        throws NamingException, JMSException {
    Properties env = new Properties();
    InitialContext jndi = new InitialContext(env);
    TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
            .lookup("topicConn");
    TopicConnection connection = conFactory.createTopicConnection(username,
            password);
    TopicSession subSession = connection.createTopicSession(false,
            Session.AUTO_ACKNOWLEDGE);
    Topic chatTopic = (Topic) jndi.lookup(topicName);
    TopicSubscriber subscriver = subSession.createSubscriber(chatTopic,
            null, true);
    Destination dest = (Destination) jndi.lookup(topicName);

    set(connection, subSession, subscriver, username, dest);
    connection.start();
}

private void set(TopicConnection connection2, TopicSession subSession2,
        TopicSubscriber subscriver2, String username2, Destination dest2) {
    this.connection = connection2;
    this.subSession = subSession2;
    this.subscriver = subscriver2;
    this.username = username2;
    this.dest = dest2;
}

public void close() throws JMSException {
    connection.close();
}

public void read() throws JMSException {

    while (true) {
        MessageConsumer consumer = subSession.createConsumer(dest);
        ;
        Message m = consumer.receive(1);

        if (m != null) {
            if (m instanceof TextMessage) {
                TextMessage message = (TextMessage) m;
                System.out.println("Reading message: " + message.getText());
            } else {
                break;
            }
        }
    }
}

public void onMessage(Message message) {
    try {
        TextMessage textMessage = (TextMessage) message;
        String text = textMessage.getText();
        System.out.println(text);
    } catch (JMSException jmse) {
        jmse.printStackTrace();
    }
}

public static void main(String[] args) throws NamingException, JMSException {
    Subscriver1 lexoMesazhin = new Subscriver1("kanaliTopic", "user",
            "user");
    lexoMesazhin.read();
    lexoMesazhin.close();
}

}

我的文件Publisher.java文件是

package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
 import javax.naming.NamingException;

public class Publisher {
private TopicSession pubSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;

public Publisher(String topicName, String username, String password)
        throws NamingException, JMSException {
    Properties env = new Properties();
    InitialContext jndi = new InitialContext(env);
    TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
            .lookup("topicConn");
    TopicConnection connection = conFactory.createTopicConnection(username,
            password);
    TopicSession pubSession = connection.createTopicSession(false,
            Session.AUTO_ACKNOWLEDGE);
    Topic chatTopic = (Topic) jndi.lookup(topicName);
    TopicPublisher publisher = pubSession.createPublisher(chatTopic);
    set(connection, pubSession, publisher, username);
    connection.start();

}

private void set(TopicConnection connection2, TopicSession pubSession2,
        TopicPublisher publisher2, String username2) {
    this.connection = connection2;
    this.pubSession = pubSession2;
    this.publisher = publisher2;
    this.username = username2;
}

protected void createMesage(String text) throws JMSException {
    TextMessage mesazhi = pubSession.createTextMessage(text);
    mesazhi.setText(username + " >> " + text);
}

public void close() throws JMSException {
    connection.close();
}

public static void main(String[] args) throws NamingException,
        JMSException, IOException {
    Publisher dergonMesazhin = new Publisher("kanaliTopic", "andi", "andi");
    BufferedReader commandLine = new java.io.BufferedReader(
            new InputStreamReader(System.in));
    try {
        while (true) {
            String s = commandLine.readLine();
            if (s.equalsIgnoreCase("exit")) {
                dergonMesazhin.close();
                System.exit(0);
            } else
                dergonMesazhin.createMesage(s);
        }
    } catch (IOException e) {

    }
}

}

2 个答案:

答案 0 :(得分:1)

您忘记发布消息,即您应该在publisher.publish(message)方法中添加电话createMesage。目前,您只是创建一个TextMessage对象而不将其发送到任何地方,因此从技术上讲,它不是您订户中的问题,而是在您的生产者中:)

答案 1 :(得分:0)

您的JNDI服务器在哪里?您将一个空的Properties对象传递给InitialContext。

Properties env = new Properties();
InitialContext jndi = new InitialContext(env);

您应该在属性对象中放置一些合理的值,例如服务器URL等,具体取决于托管JNDI服务的服务器。

另外尝试省略传递env并简单地调用new InitialContext();这将从类路径中的文件中读取正确的属性(应该在glassfish中)。