【问题标题】:How to save received messages in separate files with messagelistener如何使用 messagelistener 将收到的消息保存在单独的文件中
【发布时间】:2016-09-08 17:42:13
【问题描述】:

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();
    }
}

}

【问题讨论】:

  • 您是否正在与多个 Disk 实例共享一个 FileWriter?
  • 编辑问题以在其中包含代码。
  • 你也应该在构造函数中创建BufferedWriter,而不是每次都在onMessage()中创建一个新的。
  • 我编辑了第一篇文章
  • 包括您在其中创建 Disk 实例的代码,并将它们设置为侦听器。您使用的是Topic 而不是Queue 对吗?如果不是,那么只有一个侦听器会从同一个Queue 收到消息。

标签: java exception jms listener


【解决方案1】:

您有一个 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 是一种安全的编码方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多