Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.pptik.panwasluvillage.helpers;
- import android.os.StrictMode;
- import android.util.Log;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.util.concurrent.BlockingDeque;
- import java.util.concurrent.LinkedBlockingDeque;
- import java.util.concurrent.TimeoutException;
- import id.pptik.panwasluvillage.globalVariable.GlobalVariable;
- public class RMQ {
- GlobalVariable gb = new GlobalVariable();
- ConnectionFactory factory = new ConnectionFactory();
- /**
- * Function Connect To RMQ
- */
- public void setupConnectionFactory() {
- try {
- factory.setAutomaticRecoveryEnabled(false);
- factory.setUri("amqp://"+gb.userQueue()+":"+gb.passQueue()+"@"+gb.host());
- factory.setVirtualHost(gb.vhostRep());
- } catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException e1) {
- e1.printStackTrace();
- }
- }
- private BlockingDeque<String> queue = new LinkedBlockingDeque<String>();
- public void publishMessage(String message) {
- //Adds a message to internal blocking queue
- try {
- Log.d("","[q] " + message);
- queue.putLast(message);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- /**
- * Function Publish data ke RMQ
- * @param message
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws URISyntaxException
- * @throws IOException
- * @throws TimeoutException
- * @throws InterruptedException
- */
- public void publish(String message) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException, InterruptedException {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection connection = factory.newConnection();
- Log.d("ConnectionRMQ", "publish: "+connection.isOpen());
- Channel channel = connection.createChannel();
- Log.d("ChannelRMQ", "publish: "+channel.isOpen());
- String messageOn = message ;
- channel.basicPublish(gb.exchange(), gb.queueReport(),null,messageOn.getBytes());
- }
- public void SendSpeed() throws InterruptedException {
- Thread.sleep(500); //0.5 sec
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment