Dilan1991

Untitled

May 13th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package id.pptik.panwasluvillage.helpers;
  2. import android.os.StrictMode;
  3. import android.util.Log;
  4.  
  5. import com.rabbitmq.client.Channel;
  6. import com.rabbitmq.client.Connection;
  7. import com.rabbitmq.client.ConnectionFactory;
  8.  
  9. import java.io.IOException;
  10. import java.net.URISyntaxException;
  11. import java.security.KeyManagementException;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.util.concurrent.BlockingDeque;
  14. import java.util.concurrent.LinkedBlockingDeque;
  15. import java.util.concurrent.TimeoutException;
  16.  
  17. import id.pptik.panwasluvillage.globalVariable.GlobalVariable;
  18.  
  19. public class RMQ {
  20. GlobalVariable gb = new GlobalVariable();
  21. ConnectionFactory factory = new ConnectionFactory();
  22.  
  23. /**
  24. * Function Connect To RMQ
  25. */
  26. public void setupConnectionFactory() {
  27. try {
  28. factory.setAutomaticRecoveryEnabled(false);
  29. factory.setUri("amqp://"+gb.userQueue()+":"+gb.passQueue()+"@"+gb.host());
  30. factory.setVirtualHost(gb.vhostRep());
  31. } catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException e1) {
  32. e1.printStackTrace();
  33. }
  34. }
  35.  
  36. private BlockingDeque<String> queue = new LinkedBlockingDeque<String>();
  37. public void publishMessage(String message) {
  38. //Adds a message to internal blocking queue
  39. try {
  40. Log.d("","[q] " + message);
  41. queue.putLast(message);
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. /**
  48. * Function Publish data ke RMQ
  49. * @param message
  50. * @throws NoSuchAlgorithmException
  51. * @throws KeyManagementException
  52. * @throws URISyntaxException
  53. * @throws IOException
  54. * @throws TimeoutException
  55. * @throws InterruptedException
  56. */
  57. public void publish(String message) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException, InterruptedException {
  58.  
  59. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  60. StrictMode.setThreadPolicy(policy);
  61.  
  62. Connection connection = factory.newConnection();
  63.  
  64. Log.d("ConnectionRMQ", "publish: "+connection.isOpen());
  65. Channel channel = connection.createChannel();
  66. Log.d("ChannelRMQ", "publish: "+channel.isOpen());
  67. String messageOn = message ;
  68. channel.basicPublish(gb.exchange(), gb.queueReport(),null,messageOn.getBytes());
  69.  
  70. }
  71.  
  72. public void SendSpeed() throws InterruptedException {
  73. Thread.sleep(500); //0.5 sec
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment