简体   繁体   English

ActiveMQ:如何获取队列中的所有消息以用于Receiver(Java)

[英]ActiveMQ: How to get all messages in a queue for Receiver (Java)

Here is the Server's code: 这是服务器的代码:

import java.net.UnknownHostException;
import java.io.IOException;
import org.apache.activemq.transport.stomp.StompConnection;
public class Server{

public static void main(String[] args) {
    try {
        StompConnection con = new StompConnection();
        con.open("localhost", 61618);
        con.connect("admin", "admin123");
        con.begin("a1");
        con.send("/queue/test1", "This is test message 1");
        con.send("/queue/test1", "This is test message 2");
        con.send("/queue/test1", "This is test message 3");
        con.commit("a1");
        con.disconnect();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

Here is the Client's code: 这是客户的代码:

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Scanner;
import org.apache.activemq.transport.stomp.StompConnection;
import org.apache.activemq.transport.stomp.StompFrame;
import org.apache.activemq.transport.stomp.Stomp.Headers.Subscribe;

public class Client {

public static void main(String[] args) {
    try {
        //login.
        Scanner in = new Scanner(System.in);
        System.out.print("Password: ");
        String pass = in.next();
        if (!"123".equals(pass)){
            System.out.println("Sorry, wrong password.");

        }
        else
        {
            StompConnection con= new StompConnection();
            con.open("localhost", 61618);
            con.connect("admin", "admin123");
            con.subscribe("/queue/test1", Subscribe.AckModeValues.CLIENT);
            con.begin("a2");
            StompFrame mes = con.receive();
            System.out.println(mes.getBody());
            con.ack(message, "a2");
            con.commit("a2");               
            con.disconnect();
        }


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

I have 3 messages on Server. 我在服务器上有3条消息。 However, I only can get 1 message per time in Client. 但是,我每次只能在Client中收到1条消息。 How to get all the messages in the queue in a run? 如何在运行中获取队列中的所有消息? Anyone can help me? 有人可以帮助我吗?

Not entirely sure what you've tried here but to read all three is just a simple loop like: 不能完全确定您在这里尝试过什么,但要阅读所有这三个内容只是一个简单的循环,例如:

    con.begin("a2");
    while (true) {
        StompFrame message = null;
        try {
            message = connection.receive(5000);
        } catch (Exception e) {
            break;
        }
        System.out.println(mes.getBody());
        con.ack(message, "a2");
    }
    connection.commit("a2");                

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

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