package rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import convertors.JobConverter;
import convertors.MeasurementConverter;
import datamodel.Measurement;
import datamodel.Job;
import mongo.MongoManager;

import java.io.IOException;
import java.util.Random;


public class Sender {
    private final String hostName;
    private final String queueName;
    private Connection connection;
    private RabbitMqConfig rmq = new RabbitMqConfig();
    private Channel channel;

    public Sender() {
        hostName = rmq.getHost();
        queueName = rmq.getQueue();

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(hostName);

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            boolean durable = true;

            channel.queueDeclare(queueName, durable, false, false, null);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void Send (String clientId, String message) {
        Measurement measurement = new Measurement(clientId, message);
        String measurementString = MeasurementConverter.measurementToJsonString(measurement);

        //put data in DB
        MongoManager mm = new MongoManager();
        String id = mm.pushJson(measurementString);
        String jsonRepresentation = mm.pullJsonById(id);
        System.out.println(jsonRepresentation);
        mm.closeConnection();;

        try {
            channel.basicPublish("", queueName,
                    MessageProperties.PERSISTENT_TEXT_PLAIN, /*(message + i).getBytes()*/
                    //ObjToJsonConvertor.map(new Job("ls -l")).getBytes());
                    JobConverter.jobToJsonString(new Job(id)).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void closeConnection() {

        try {
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

@@ -361,33 +361,8 @@
 on;%0A
-import java.util.Random;%0A
 %0A%0Apu
@@ -572,16 +572,74 @@
 annel;%0A%0A
+    /**%0A     * Set parameters for RabbitMQ sender%0A     */%0A
     publ
@@ -1122,32 +1122,173 @@
       %7D%0A%0A    %7D%0A%0A
+    /**%0A     * Send messages over queue and DB.%0A     * @param clientId id of the client%0A     * @param message command to be executed%0A     */%0A
     public void 
@@ -1750,17 +1750,16 @@
 ction();
-;
 %0A%0A      
@@ -1878,113 +1878,8 @@
 AIN,
- /*(message + i).getBytes()*/%0A                    //ObjToJsonConvertor.map(new Job(%22ls -l%22)).getBytes());
 %0A   
@@ -2152,24 +2152,91 @@
   %7D%0A%0A    %7D%0A%0A
+    /**%0A     * Close connection used for sending messages.%0A     */%0A
     public v

Add comments.