package lc.esp.sdk;

import lc.mecha.util.BasicallyDangerous;

import javax.jms.Message;
import javax.jms.MessageProducer;
import java.util.concurrent.LinkedBlockingQueue;

public class AsyncSender extends BasicallyDangerous {
    private final MessageProducer producer;
    private final LinkedBlockingQueue<Message> q = new LinkedBlockingQueue<>();

    public AsyncSender(MessageProducer producer) {
        this.producer = producer;
    }

    public void send(Message msg) {
        q.add(msg);
    }

    @Override
    public void runDangerously() throws Exception {
        //noinspection InfiniteLoopStatement
        while (true) {
            Message msg = q.take();
            producer.send(msg);
        }
    }
}
