osipyonok

SP_Lab4_CALC_G

Dec 25th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 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.ExceptionListener;
  6. import javax.jms.JMSException;
  7. import javax.jms.Message;
  8. import javax.jms.MessageConsumer;
  9. import javax.jms.MessageProducer;
  10. import javax.jms.Session;
  11. import javax.jms.TextMessage;
  12. import org.apache.activemq.ActiveMQConnectionFactory;
  13.  
  14. public class G implements Runnable, ExceptionListener{
  15.     private final String brokerUrl;
  16.     private final String topicName = "G";
  17.  
  18.     public G(String brokerUrl){
  19.         this.brokerUrl = brokerUrl;
  20.     }
  21.    
  22.     public void run(){
  23.         try
  24.         {
  25.             ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
  26.             Connection connection = connectionFactory.createConnection();
  27.             connection.start();
  28.             connection.setExceptionListener(this);
  29.             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  30.             Destination destination = session.createTopic(topicName);
  31.             MessageConsumer consumer = session.createConsumer(destination);
  32.             MessageProducer replyProducer = session.createProducer(null);
  33.             replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  34.             Message message = consumer.receive();
  35.             if (message == null)
  36.                     return;
  37.             if (message instanceof TextMessage){
  38.                 TextMessage textMessage = (TextMessage) message;            
  39.                 String text = textMessage.getText();
  40.                 TextMessage response = session.createTextMessage();
  41.                 Thread.sleep(66);
  42.            //       Random random = new Random();
  43.                 String cur = message.getJMSDestination().toString();
  44.                 cur = cur.substring(cur.length() - 1);
  45.                 int wait = 0;// Math.abs((random.nextInt() % 10000)) + 1000;
  46.                 wait = 25000;
  47.             //    Thread.sleep(66);
  48.                 Thread.sleep(wait);
  49.                 Boolean ans = Integer.parseInt(text) <= 10;
  50.                 response.setText(ans.toString());
  51.                 response.setJMSCorrelationID(cur);
  52.                 replyProducer.send(message.getJMSReplyTo() , response);
  53.             }
  54.             consumer.close();
  55.             session.close();
  56.             connection.close();
  57.             return;
  58.         }
  59.         catch (Exception e)
  60.         {
  61.         }
  62.     }
  63.    
  64.     public synchronized void onException(JMSException ex)
  65.     {
  66.         ex.printStackTrace();
  67.     }
  68.  
  69. }
Add Comment
Please, Sign In to add comment