package amq;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.jboss.rhiot.beacon.publishers.qpid.QpidPublisher;
import org.junit.Test;

/**
 * Created by starksm on 8/7/15.
 */
public class TestJMSClient {
    private static final String USER = "demo-user";
    private static final String PASSWORD = "2015-summit-user";

    static void sendmsg(Session session, Destination dest) throws JMSException {
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        Message msg = session.createTextMessage();
        msg.setStringProperty("scannerID", "LoopbackTest");
        producer.send(msg);
        System.out.printf("Sent message\n");
        producer.close();
    }
    static void recvmsg(Session session, Destination dest) throws JMSException {
        MessageConsumer consumer = session.createConsumer(dest);
        Message msg = consumer.receive(5000);
        System.out.printf("Recv message: %s\n", msg);
        consumer.close();
    }
    @Test
    public void testSendRecv() throws Exception {
        Properties props = new Properties();
        props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
        //props.setProperty("connectionfactory.myFactoryLookup", "amqp://52.10.252.216:5672");
        props.setProperty("connectionfactory.myFactoryLookup", "amqp://192.168.1.107:5672");
        Context context = new InitialContext(props);

        // Create a Connection
        ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
        Connection connection = factory.createConnection(USER, PASSWORD);
        System.out.printf("ConnectionFactory created connection: %s\n", connection);
        connection.setExceptionListener(new ExceptionListener() {
            @Override
            public void onException(JMSException ex) {
                ex.printStackTrace();
            }
        });
        connection.start();

        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        System.out.printf("Created session: %s\n", session);
        Destination beaconEvents  = session.createQueue("beaconEvents");
        sendmsg(session, beaconEvents);
        recvmsg(session, beaconEvents);
        session.close();
        connection.close();
    }

    @Test
    public void testSendRecvFailoverURI() throws Exception {
        Properties props = new Properties();
        props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
        //props.setProperty("connectionfactory.myFactoryLookup", "amqp://52.10.252.216:5672");
        String uri = String.format("failover:(%s)?failover.reconnectDelay=1000", "amqp://192.168.1.107:5672");
        props.setProperty("connectionfactory.myFactoryLookup", uri);
        Context context = new InitialContext(props);

        // Create a Connection
        ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
        Connection connection = factory.createConnection(USER, PASSWORD);
        System.out.printf("ConnectionFactory created connection: %s\n", connection);
        connection.setExceptionListener(new ExceptionListener() {
            @Override
            public void onException(JMSException ex) {
                ex.printStackTrace();
            }
        });
        connection.start();

        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        System.out.printf("Created session: %s\n", session);
        Destination beaconEvents  = session.createQueue("beaconEvents");
        sendmsg(session, beaconEvents);
        recvmsg(session, beaconEvents);
        session.close();
        connection.close();
    }


    @Test
    public void testQpidPublisher() throws Exception {
        //String uri = String.format("failover:(%s)?failover.reconnectDelay=1000", "amqp://192.168.1.107:5672");
        String uri = "amqp://192.168.1.107:5672";

        QpidPublisher publisher = new QpidPublisher(uri, USER, PASSWORD, null);
        publisher.setDestinationName("beaconEvents");
        publisher.start(false);
        Properties props = new Properties();
        props.setProperty("scannerID", "testQpidPublisher");
        publisher.publishProperties(null, props);
        Message msg = publisher.recvMessage();
        System.out.printf("%s\n", msg);
        publisher.stop();
    }
}

@@ -4848,11 +4848,1475 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testBeaconEventsRecv() throws Exception %7B%0A        Properties props = new Properties();%0A        props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, %22org.apache.qpid.jms.jndi.JmsInitialContextFactory%22);%0A        props.setProperty(%22connectionfactory.myFactoryLookup%22, %22amqp://192.168.1.107:5672%22);%0A        Context context = new InitialContext(props);%0A%0A        // Create a Connection%0A        ConnectionFactory factory = (ConnectionFactory) context.lookup(%22myFactoryLookup%22);%0A        Connection connection = factory.createConnection(USER, PASSWORD);%0A        System.out.printf(%22ConnectionFactory created connection: %25s%5Cn%22, connection);%0A        connection.setExceptionListener(new ExceptionListener() %7B%0A            @Override%0A            public void onException(JMSException ex) %7B%0A                ex.printStackTrace();%0A            %7D%0A        %7D);%0A        connection.start();%0A%0A        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);%0A        System.out.printf(%22Created session: %25s%5Cn%22, session);%0A        Destination beaconEvents  = session.createTopic(%22beaconEvents%22);%0A        MessageConsumer consumer = session.createConsumer(beaconEvents);%0A        Message msg = consumer.receive(31000);%0A        while(msg != null) %7B%0A            System.out.printf(%22Recv message: %25s%5Cn%22, msg);%0A            msg = consumer.receive(31000);%0A        %7D%0A        consumer.close();%0A        session.close();%0A        connection.close();%0A    %7D
 %0A%7D%0A

Add testBeaconEventsRecv