2013-10-30 4 views
0

StringBuilder는 수신기에 3 개의 XML 형식의 개체를 보내는 사람 (이 예제에서는)이지만, 내 stringbuilder는 첫 번째 개체를 먼저 인쇄 한 다음 첫 번째 개체를 인쇄 한 다음 첫 번째 개체를 인쇄 한 다음 세 개체 모두를 인쇄합니다 대신 : - 두 번째 을 인쇄 - - 첫째 을 인쇄 셋째ActiveMQ의 Stringbuilder

인쇄하여이 내 코드입니다 : 당신의 생산에서

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

답변

1

, 당신이 당신의 작가를 초기화하지 다시 않았다. 따라서 보내는 메시지에는 실제로 첫 번째 객체 인 첫 번째 객체와 두 번째 객체가 포함 된 다음 세 개의 객체가 모두 포함됩니다. 생성자 코드를 변경하여 for 루프 내부에서 StringWriter를 초기화합니다.

+0

고마워요! 네가 옳아 –