Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bolid;
- import java.util.Date;
- import javax.annotation.Resource;
- import javax.ejb.CreateException;
- import javax.ejb.Schedule;
- import javax.ejb.Singleton;
- import javax.ejb.Startup;
- import javax.ejb.Stateless;
- import javax.ejb.Timer;
- import javax.jms.ConnectionFactory;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageProducer;
- import javax.jms.ObjectMessage;
- import javax.jms.QueueConnection;
- import javax.jms.QueueSession;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- @Singleton
- @Startup
- public class BolidStatsSingletonTimer {
- @Resource(lookup = "java:/ConnectionFactory")
- ConnectionFactory connectionFactory;
- @Resource(lookup = "java:/jms/queue/SRIQueue")
- Destination destination;
- public BolidStatsSingletonTimer() {
- }
- @Schedule(second = "*/15", minute = "*", hour = "*", dayOfWeek = "*", dayOfMonth = "*",
- month = "*", year = "*", info = "MyTimer", persistent = false)
- private void scheduledTimeout(final Timer t) {
- sendStatistics();
- }
- public void sendStatistics() {
- QueueConnection queueConnection = null;
- QueueSession session = null;
- MessageProducer producer = null;
- try {
- queueConnection = (QueueConnection) connectionFactory.createConnection();
- session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
- producer = session.createProducer(destination);
- ObjectMessage objectMessage = createStatistics(session);
- producer.send(objectMessage);
- }
- catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- try {
- producer.close();
- session.close();
- queueConnection.close();
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public ObjectMessage createStatistics(Session session) {
- ObjectMessage objectMessage = null;
- BolidStats bolidStats = new BolidStats((Math.random() * 100) + 50, Math.random() * 1000, Math.random() * 1000, new Date());
- try {
- objectMessage = session.createObjectMessage();
- objectMessage.setObject(bolidStats);
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return objectMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement