// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import com.rabbitmq.jms.admin.RMQConnectionFactory;
import com.rabbitmq.jms.admin.RMQDestination;

public class Send2 {
    // for RabbitMQ:
    private static final String HOST = "localhost";
    private static final int PORT = 5672;
    private static final String UN = "guest";
    private static final String PW = "guest";
    // for all:
    private static final String QNAME = "TestQ";
    private static final String PAYLOAD = "<data>\r\n    <id>123</id>\r\n    <name>ABC</name>\r\n</data>";
    public static void test(QueueConnectionFactory qcf) throws JMSException {
        QueueConnection con = qcf.createQueueConnection(UN, PW);
        con.start();
        QueueSession ses = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue q = new RMQDestination(QNAME, "", QNAME, null);
        QueueSender sender = ses.createSender(q);
        for(int i = 0; i < 3; i++) {
            Message smsg = ses.createTextMessage(PAYLOAD);
            sender.send(smsg);
        }
        ses.close();
        sender.close();
        con.close();
        
    }
    public static void main(String[] args) throws JMSException {
        RMQConnectionFactory cf = new RMQConnectionFactory();
        cf.setHost(HOST);
        cf.setPort(PORT);
        test(cf);
    }
}
