-2

I don't think to use a while (true) inside a servlet could be a good idea, so I wonder if I should useServlet With Thread or what other alternatives I have for this.

@Override
    public void doWork(Channel channel, QueueingConsumer queueingConsumer)
            throws Exception {
        while (true) {
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build();
            byte[] response = null;
            try {

                String message = ObjectCodec.deSerialize(delivery.getBody()).toString();

                response = process(message);
            } catch (Exception e) {
                LogUtils.logError("CMDBDeleteQueue process data fail.", e);
            } finally {
                channel.basicPublish("", props.getReplyTo(), replyProps,response);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    }

There is no other way,Do not apply while(true)? thanks.

jack
  • 113
  • 2
  • 9
  • You haven't told us what you are trying to do, or where this lives in your servlet, or why you've put it in a servlet. – bmargulies Apr 09 '15 at 01:38
  • RabbitMQ consumers does not belong in a servlet. RabbitMQ consumers consume messages and act on it. A servlet is a request processor. None of those belong in the same class, not even in the same projects if your application is well designed. – Jazzwave06 Apr 09 '15 at 01:41
  • rabbitMQ prc.Waiting for the message, use implementation.There is no other way to replace the while (true) – jack Apr 09 '15 at 02:21
  • RPC you mean. What are you calling? Both a servlet and a RabbitMQ consumer can be RPC request servers. You shouldn't have both tough. – Jazzwave06 Apr 09 '15 at 02:25
  • You should implement DefaultConsumer, please read: http://stackoverflow.com/questions/22840247/rabbitmq-java-client-using-defaultconsumer-vs-queueingconsumer – Gabriele Santomaggio Apr 09 '15 at 12:39

2 Answers2

0

Both a servlet and a RabbitMQ consumer can be RPC servers. In pseudo code,

With RabbitMQ:

while isConsuming
    var message = wait message
    handler.handleAsync(message)

With Servlets:

void doGet()
    var message = request.getContent()
    handler.handleAsync(message)

Clients of the RPC with RabbitMQ:

var message = new()
publisher.send(message)

Clients of the RPC with Servlets:

var httpRequest = new()
httpRequest.setContent()
httpRequest.send()
Jazzwave06
  • 1,883
  • 1
  • 11
  • 19
  • Ok look, I think you simply don't understand how the whole thing work and might not be the good person to develop the rpc server. You don't need to know what the send code looks like. It's an http request. Thing is when you develop a servlet, the while (true) loop is implicit and is given by the aplication server. The application server (Tomcat for example) will loop, listening on port 80, waiting for http requests to parse and pipeline through to your servlet. – Jazzwave06 Apr 09 '15 at 02:43
  • ya.
    The functionality is implemented. i mean There is no other way to replace the while (true). Optimize the code.
    – jack Apr 09 '15 at 02:43
  • You lack imagination. There's about a thousand way to eliminate that while loop. Anyway, just put the consumer on a dedicated thread if you just want to patch. – Jazzwave06 Apr 09 '15 at 02:43
  • Eh.I am a synchronous way.Need to return data. thread run method is void methd. – jack Apr 09 '15 at 02:48
  • Then clearly you have no clue what you are doing. You DON'T run a consumer in a synchronous way. – Jazzwave06 Apr 09 '15 at 02:49
0

Given your ostensble knowledge you should seriously consider looking at something like spring-amqp which will help with the necessary resource and threadpool management required for consuming a queue which actually is sort of nontrivial particularly if you are going to be doing RPC.

http://projects.spring.io/spring-amqp/

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • You could have a look too at [Spring Integration DSL](https://github.com/spring-projects/spring-integration-java-dsl/wiki/Spring-Integration-Java-DSL-Reference) which ease rabbitmq integration – Nicolas Labrot Apr 09 '15 at 06:46