

import java.util.Iterator;
import java.util.Set;
import javax.jms.Connection;
import javax.jms.ConnectionMetaData;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.advisory.DestinationListener;
import org.apache.activemq.advisory.DestinationSource;
import org.apache.activemq.broker.region.Queue;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTempQueue;
import org.apache.activemq.command.ActiveMQTopic;

/**
 *
 * @author David Rodriguez A.
 */

public class MsgToReactorControl implements Runnable, MessageListener {

    private ActiveMQConnection connection;
    private ActiveMQConnectionFactory connectionFactory;
    private String targetQueue;

    public MsgToReactorControl(String target){
        this.targetQueue="ControlQueue:"+target;
    }


    public boolean sendMsg() {
        boolean retorno = false;
        try {
            connectionFactory = new ActiveMQConnectionFactory("system", "manager", "tcp://localhost:61616");
            connection = (ActiveMQConnection) connectionFactory.createConnection();
            connection.start();
            
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(this.targetQueue);
            MessageProducer producer = session.createProducer(destination);

            //TextMessage message = session.createTextMessage(mensaje);
            MapMessage msg = session.createMapMessage();
            msg.setString("CONTROL","on/off");
            producer.send(msg);
            producer.close();
            session.close();
            connection.cleanup();
            connection.close();
            
            retorno = true;
        } catch (JMSException ex) {
            retorno = true;
            System.out.println(ex.getMessage());
        }
        return retorno;
    }

    public void run() {
        System.out.println("Sending  msg to ReactorControl ...\n");
        sendMsg();
    }

    public void onMessage(Message msg) {
        

    }
}
