Java 一旦消费者读取数据,如何停止/杀死消费者?

Java 一旦消费者读取数据,如何停止/杀死消费者?,java,Java,我使用发布者/订阅者模型以循环方式传递消息。这里我使用2个消费者来处理请求。我想在一个消费者处理一个请求时停止它。如何阻止一个消费者?如果是,剩余的请求将转到其他使用者。如果使用者无法发送回,那么(持久)数据会发生什么情况?它将丢失还是交付给其他消费者 public class Testing { public static void main(String[] args) throws URISyntaxException, Exception { BrokerSer

我使用发布者/订阅者模型以循环方式传递消息。这里我使用2个消费者来处理请求。我想在一个消费者处理一个请求时停止它。如何阻止一个消费者?如果是,剩余的请求将转到其他使用者。如果使用者无法发送回,那么(持久)数据会发生什么情况?它将丢失还是交付给其他消费者

public class Testing {

    public static void main(String[] args) throws URISyntaxException, Exception {
        BrokerService broker = BrokerFactory.createBroker(new URI(
                "broker:(tcp://localhost:61616)"));
        broker.start();
        Connection connection = null;
        try {
            // Producer
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    "tcp://localhost:61616");
            connection = connectionFactory.createConnection();
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("customerQueue");

            // Consumer
            for (int i = 0; i < 2; i++) {
                MessageConsumer consumer = session.createConsumer(queue);
                if(i == 0)
                {
                consumer.setMessageListener(new ConsumerMessageListener("Consumer " + i));
                }


                consumer.setMessageListener(new TestingOnc("Consumer" + i));


                }

            broker.stop();
        }
    }

}
公共类测试{
publicstaticvoidmain(字符串[]args)抛出URISyntaxException,异常{
BrokerService broker=BrokerFactory.createBroker(新URI(
“经纪人:(tcp://localhost:61616)"));
broker.start();
连接=空;
试一试{
//制作人
ConnectionFactory ConnectionFactory=新的ActiveMQConnectionFactory(
"tcp://localhost:61616");
connection=connectionFactory.createConnection();
Session Session=connection.createSession(false,
会话(自动确认);
Queue Queue=session.createQueue(“customerQueue”);
//消费者
对于(int i=0;i<2;i++){
MessageConsumer=session.createConsumer(队列);
如果(i==0)
{
consumer.setMessageListener(新ConsumerMessageListener(“consumer”+i));
}
consumer.setMessageListener(新的TestingOnc(“consumer”+i));
}
broker.stop();
}
}
}
我正在用的制片人电话

public synchronized static void createMQRequestForPoster(Long zoneId, List<String> postJobs, int priority) throws JMSException {

        Connection connection = null;
        try {
            connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(false,
                    Session.CLIENT_ACKNOWLEDGE);
            Destination dest = session
                    .createQueue(SMCConstants.ACTIVEMQ_POST_REQUEST);
            MessageProducer producer = session.createProducer(dest);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);

            logger.info("List of jobs adding into poster queue: "+postJobs.size());
            for(String str : postJobs) {

                TextMessage message = session.createTextMessage();
                JSONObject obj = new JSONObject();
                obj.put("priority", priority);
                obj.put("zoneId", zoneId);
                obj.put("postJobs", str);
                logger.debug(obj.toString());
                message.setText(obj.toString());
                message.setIntProperty(ActiveMQReqProcessor.REQ_TYPE, 0);
                producer.send(message);
            }
        } catch (JMSException | JSONException e) {
            logger.warn("Failed to add poster request to ActiveMq", e);
        } finally {
            if(connection != null)
                connection.close();
        }
    }
}
public synchronized static void createMQRequestForPoster(Long zoneId,List postJobs,int priority)抛出JMSException{
连接=空;
试一试{
connection=factory.createConnection();
connection.start();
Session Session=connection.createSession(false,
会议。客户确认);
目的地dest=会话
.createQueue(smcstants.ACTIVEMQ_POST_请求);
MessageProducer=session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
logger.info(“添加到海报队列中的作业列表:“+postJobs.size());
for(字符串str:postJobs){
TextMessage=session.createTextMessage();
JSONObject obj=新的JSONObject();
“优先权”,优先权;
对象put(“区域ID”,区域ID);
对象put(“postJobs”,str);
debug(obj.toString());
message.setText(obj.toString());
message.setIntProperty(ActiveMQReqProcessor.REQ_类型,0);
生产者。发送(消息);
}
}catch(jmscexception | JSONException e){
logger.warn(“未能向ActiveMq添加海报请求”,e);
}最后{
if(连接!=null)
connection.close();
}
}
}
我能够发送和接收信息