Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package bolid;
  2.  
  3. import java.util.Date;
  4.  
  5. import javax.annotation.Resource;
  6. import javax.ejb.CreateException;
  7. import javax.ejb.Schedule;
  8. import javax.ejb.Singleton;
  9. import javax.ejb.Startup;
  10. import javax.ejb.Stateless;
  11. import javax.ejb.Timer;
  12. import javax.jms.ConnectionFactory;
  13. import javax.jms.Destination;
  14. import javax.jms.JMSException;
  15. import javax.jms.Message;
  16. import javax.jms.MessageProducer;
  17. import javax.jms.ObjectMessage;
  18. import javax.jms.QueueConnection;
  19. import javax.jms.QueueSession;
  20. import javax.jms.Session;
  21. import javax.jms.TextMessage;
  22.  
  23. @Singleton
  24. @Startup
  25. public class BolidStatsSingletonTimer {
  26.  
  27.     @Resource(lookup = "java:/ConnectionFactory")
  28.     ConnectionFactory connectionFactory;
  29.  
  30.     @Resource(lookup = "java:/jms/queue/SRIQueue")
  31.     Destination destination;
  32.  
  33.     public BolidStatsSingletonTimer() {
  34.     }
  35.  
  36.     @Schedule(second = "*/15", minute = "*", hour = "*", dayOfWeek = "*", dayOfMonth = "*",
  37.             month = "*", year = "*", info = "MyTimer", persistent = false)
  38.     private void scheduledTimeout(final Timer t) {
  39.         sendStatistics();
  40.  
  41.     }
  42.  
  43.     public void sendStatistics() {
  44.         QueueConnection queueConnection = null;
  45.         QueueSession session = null;
  46.         MessageProducer producer = null;
  47.        
  48.         try {
  49.             queueConnection = (QueueConnection) connectionFactory.createConnection();
  50.             session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  51.             producer = session.createProducer(destination);
  52.             ObjectMessage objectMessage = createStatistics(session);
  53.             producer.send(objectMessage);
  54.  
  55.         }      
  56.         catch (JMSException e) {
  57.             // TODO Auto-generated catch block
  58.             e.printStackTrace();
  59.         }finally {
  60.             try {
  61.                 producer.close();
  62.                 session.close();
  63.                 queueConnection.close();
  64.             } catch (JMSException e) {
  65.                 // TODO Auto-generated catch block
  66.                 e.printStackTrace();
  67.         }
  68.     }
  69.     }
  70.    
  71.     public ObjectMessage createStatistics(Session session) {
  72.         ObjectMessage objectMessage = null;
  73.         BolidStats bolidStats = new BolidStats((Math.random() * 100) + 50, Math.random() * 1000, Math.random() * 1000, new Date());
  74.         try {
  75.             objectMessage = session.createObjectMessage();
  76.             objectMessage.setObject(bolidStats);
  77.         } catch (JMSException e) {
  78.             // TODO Auto-generated catch block
  79.             e.printStackTrace();
  80.         }
  81.         return objectMessage;
  82.        
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement