简体   繁体   English

ActiveMQ中的Stringbuilder

[英]Stringbuilder in ActiveMQ

So I have a sender who sends (in this example) 3 xml-formatted objects to a receiver, however my stringbuilder is first printing out the first object, then the first + the second, then all 3 of them, instead of: - Print out first - Print out second - Print out third 因此,我有一个发送方(在本示例中)将3个xml格式的对象发送给接收方,但是我的stringbuilder首先打印出第一个对象,然后打印第一个+第二个对象,然后打印所有三个对象,而不是:-打印首先打印-第二打印-第三打印

This is my code: 这是我的代码:

public class TrackingSender {
public static void main(String[] args) {
    try {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue("TEST.SENDRECEIVE");

        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        TrackingInformatieFactory.makeTrackingInformatieList();
        List<TrackingInformatie> trackingInformatieList = TrackingInformatieFactory.getTrackingInformatieList();
        Writer writer = new StringWriter();
        TextMessage message;
        for(TrackingInformatie ti: trackingInformatieList){
            Marshaller.marshal(ti, writer);
            message = session.createTextMessage(writer.toString());
            producer.send(message);
            Thread.sleep(1000);
        }

    } catch(Exception e){
        e.printStackTrace();
    }
}

}

public class TrackingReceiver {
public static void main(String[] args) {
    try {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("TEST.SENDRECEIVE");
        MessageConsumer consumer = session.createConsumer(destination);
        TextMessage message;
        Reader reader;
        StringBuilder builder;
        int charsRead;
        char[] chars;
        TrackingInformatieFactory.makeTrackingInformatieList();
        List<TrackingInformatie> trackingInformatieList = TrackingInformatieFactory.getTrackingInformatieList();
        for (int i = 0; i < trackingInformatieList.size(); i++){
            message = (TextMessage) consumer.receive();
            reader = new StringReader(message.getText());
            builder = new StringBuilder();
            chars = new char[100];
            charsRead = -1;
        do{
            charsRead = reader.read(chars,0,chars.length);
            //if we have valid chars, append them to end of string.
            if(charsRead>0)
                builder.append(chars,0,charsRead);
        }
        while(charsRead>0);
            String stringReadFromReader = builder.toString();
            System.out.println("String read = " + stringReadFromReader);

        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
} 

In your producer, you never re-initialize your writer. 在生产者中,您永远不会重新初始化您的作家。 So the messages you send actually contain the first object, the first and second object and then all three. 因此,您发送的消息实际上包含第一个对象,第一个和第二个对象,然后是所有三个。 Change your producer code to initialize the StringWriter inside the for loop instead of before it. 更改生产者代码,以在for循环中而不是在其之前初始化StringWriter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM