Advertisement
osipyonok

SP_Lab4_TopicProducer

Dec 25th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import java.util.Random;
  2. import javax.jms.Connection;
  3. import javax.jms.DeliveryMode;
  4. import javax.jms.Destination;
  5. import javax.jms.MessageProducer;
  6. import javax.jms.Session;
  7. import javax.jms.Message;
  8. import javax.jms.MessageConsumer;
  9. import javax.jms.MessageListener;
  10. import javax.jms.TextMessage;
  11. import org.apache.activemq.ActiveMQConnectionFactory;
  12.  
  13. public class TopicProducer implements Runnable, MessageListener{
  14.     static boolean done_f = false;
  15.     static boolean done_g = false;
  16.     static boolean result_f = false;
  17.     static boolean result_g = false;
  18.     public static boolean global_done = false;
  19.     public static boolean global_result = false;
  20.     private final String brokerUrl;
  21.     public final String topicName;
  22.     String x;
  23.    
  24.  
  25.     public TopicProducer(String brokerUrl, String topicName , Integer _x)
  26.     {
  27.         this.brokerUrl = brokerUrl;
  28.         this.topicName = topicName;
  29.         x = _x.toString();
  30.     }
  31.    
  32.     public void run()
  33.     {
  34.         try
  35.         {
  36.             ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
  37.             Connection connection = connectionFactory.createConnection();
  38.             connection.start();
  39.             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//Session.AUTO_ACKNOWLEDGE
  40.             Destination destination = session.createTopic(topicName);
  41.             Destination temp_dest = session.createTemporaryTopic();
  42.             MessageConsumer responseConsumer = session.createConsumer(temp_dest);
  43.             responseConsumer.setMessageListener(this);
  44.             MessageProducer producer = session.createProducer(destination);    
  45.             producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);    
  46.             String text = x;
  47.             TextMessage message = session.createTextMessage(text);
  48.             message.setJMSReplyTo(temp_dest);
  49.             String correlationId = this.createRandomString();
  50.             message.setJMSCorrelationID(correlationId);
  51.             producer.send(message);
  52.             for(int i = 0 ; i < 1 ; i %= 1){
  53.                 Thread.sleep(500);
  54.                 Boolean res = result_f && result_g;
  55.                 if(done_f && done_g){
  56.                     global_done = true;
  57.                     global_result = res;
  58.                     break;
  59.                 }
  60.                 if(done_f && !result_f){
  61.                     global_done = true;
  62.                     global_result = false;
  63.                     break;
  64.                 }
  65.                 if(done_g && !result_g){
  66.                     global_done = true;
  67.                     global_result = false;
  68.                     break;
  69.                 }
  70.             }
  71.             session.close();
  72.             connection.close();
  73.         }
  74.         catch (Exception e)
  75.         {
  76.         }
  77.     }
  78.    
  79.     public void onMessage(Message message) {
  80.         try{
  81.             if (message == null)
  82.                 return;
  83.             if (message instanceof TextMessage){
  84.                 TextMessage textMessage = (TextMessage) message;            
  85.                 String text = textMessage.getText();
  86.                 if(message.getJMSCorrelationID().equals("F")){
  87.                     done_f = true;
  88.                     result_f = Boolean.parseBoolean(text);
  89.                 //  System.out.println("f(" + x + ") = " + text);
  90.                 }else{
  91.                     done_g = true;
  92.                     result_g = Boolean.parseBoolean(text);
  93.                 //  System.out.println("g(" + x + ") = " + text);
  94.                 }
  95.             }
  96.         }catch (Exception e)
  97.         {
  98.         }
  99.     }
  100.    
  101.     private String createRandomString() {
  102.         Random random = new Random(System.currentTimeMillis());
  103.         long randomLong = random.nextLong();
  104.         return Long.toHexString(randomLong);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement