xyz.cloudbans.barker.amqp.BarkerConsumer.java Source code

Java tutorial

Introduction

Here is the source code for xyz.cloudbans.barker.amqp.BarkerConsumer.java

Source

/*
 * Copyright 2016 CloudBans (https://cloudbans.xyz)
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package xyz.cloudbans.barker.amqp;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import xyz.cloudbans.barker.Message;
import xyz.cloudbans.barker.Subscription;
import xyz.cloudbans.barker.exception.BarkerException;
import xyz.cloudbans.barker.listener.BarkerListener;

import java.io.IOException;
import java.util.Arrays;

public class BarkerConsumer extends DefaultConsumer {

    private final Object lock = new Object();
    private final BarkerListener listener;
    private volatile Subscription subscription;

    public BarkerConsumer(Channel channel, BarkerListener listener) {
        super(channel);

        this.listener = listener;
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
        synchronized (lock) {
            lock.notify();
        }
    }

    @Override
    public void handleDelivery(String consumerTag, final Envelope envelope, AMQP.BasicProperties properties,
            final byte[] body) throws IOException {
        // Block until subscription is available
        if (subscription == null) {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException ignore) {
                }
            }
        }

        listener.onMessage(subscription, new Message() {
            @Override
            public void acknowledge() {
                try {
                    getChannel().basicAck(envelope.getDeliveryTag(), false);
                } catch (IOException e) {
                    throw new BarkerException("An exception occurred while acknowledging message.", e);
                }
            }

            @Override
            public void requeue() {
                try {
                    getChannel().basicReject(envelope.getDeliveryTag(), true);
                } catch (IOException e) {
                    throw new BarkerException("An exception occurred while requeue message.", e);
                }
            }

            @Override
            public byte[] getBody() {
                return Arrays.copyOf(body, body.length);
            }
        });
    }
}