如何使用messagelistener

时间:2016-09-08 17:42:13

标签: java exception jms listener

Disk.class实现

package server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import services.CustomerData;

public class Disk implements MessageListener{

private int index;
private FileWriter f;
private BufferedWriter b;

public Disk(int i){
    this.index=i;
    try {
        f = new FileWriter("disk"+i+".txt",true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    b = new BufferedWriter(f);
}

@Override
public void onMessage(Message m) {
    try {
        if(m instanceof ObjectMessage){
            CustomerData c = (CustomerData) ((ObjectMessage) m).getObject();
            b.write(c.getSurname()+" "+c.getName()+" "+c.getAge());
            b.newLine();
            b.flush();
            System.out.println("disk"+index+".txt saved");
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

所以,会发生的是每个消息监听器收到的每条消息都保存在同一个文件(最后一个索引的disk.txt文件)中,但我想将它们保存在每个文件中,从0到N.N txt文件已创建,但除最后一个之外,它们不会被修改。

编辑:我在磁盘构造函数中添加了FileWriter和BufferedWriter,但它将创建N个文件但仅修改最后一个文件。

创建磁盘的主类:

package server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class StorageServer {

public static final int N = 10;

public static void main(String[] args) throws RemoteException {
    Hashtable<String,String> prop = new Hashtable<String,String>();
    prop.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    prop.put("java.naming.provider.url", "tcp://127.0.0.1:61616");
    prop.put("topic.req", "requests");
    System.setProperty("org.apache.activemq.SERIALIZABLE_PACKAGES","*");
    try {
        Context jndiCon = new InitialContext(prop);
        TopicConnectionFactory tConnFact = (TopicConnectionFactory) jndiCon.lookup("TopicConnectionFactory");
        TopicConnection tConn = tConnFact.createTopicConnection();
        TopicSession tSess = tConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = (Topic) jndiCon.lookup("req");
        TopicSubscriber subscriber = tSess.createSubscriber(topic);
        tConn.start();
        for(int i=0; i<N; i++){
            //FileWriter file = new FileWriter("disk"+i+".txt",true);
            subscriber.setMessageListener(new Disk(i));
            System.out.println("New disk"+i+" started");
        }
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您有一个TopicSubscriber,其中只有一个MessageListener(因此setMessageListener而非addMessageListener)。您需要使用

为每个侦听器创建单独的TopicSubscriber
for(int i=0; i<N; i++){
    TopicSubscriber subscriber = tSess.createSubscriber(topic);
    subscriber.setMessageListener(new Disk(i));
    System.out.println("New disk"+i+" started");
}

我还建议避免使用FileWriter(和FileReader)类,因为它使用平台编码。当平台(或其编码)发生变化时,这可能会导致意外。等效但更长更安全的方式是:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("whatever.txt"), "UTF-8"));

使用UTF-8是一种安全的编码方式。