All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lv.ctco.cukes.rabbitmq.internal.MessageService Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package lv.ctco.cukes.rabbitmq.internal;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

@Singleton
public class MessageService {

    @Inject
    ConnectionService connectionService;

    @SneakyThrows(IOException.class)
    public void sendMessage(String exchange, String routingKey, MessageWrapper message) {
        Channel channel = connectionService.getChannel();
        channel.basicPublish(exchange, routingKey, message.getProperties().build(), message.getBody().getBytes());
    }

    @SneakyThrows({IOException.class, InterruptedException.class})
    public MessageWrapper receiveMessage(String queue, int timeoutInSeconds) {
        Channel channel = connectionService.getChannel();
        BlockingQueue result = new ArrayBlockingQueue(1);
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String response = new String(body);
                MessageWrapper messageWrapper = new MessageWrapper(response, properties);
                result.add(messageWrapper);
            }
        });
        return result.poll(timeoutInSeconds, TimeUnit.SECONDS);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy