JBoss Community

Singleton Timerbean cannot be installed

created by Ulrich Kriegel in Beginner's Corner - View the full discussion

Hi there,

I'm new here and hope thats the correct forum to ask. If not, please, give me a a link to the correct one.

 

I'm trying to install a simple timer bean in JBoss6.1.Final, which sould publish every 10 sec a Testmessage with content "foo" on a given Topic

 

package foo.baz;

 

import javax.annotation.Resource;

import javax.ejb.LocalBean;

import javax.ejb.Schedule;

import javax.ejb.Singleton;

import javax.ejb.Stateless;

import javax.ejb.Timeout;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.jms.Topic;

 

 

import org.apache.log4j.Logger;

 

 

/**

* Session Bean implementation class LifeEventTimer

*/

//@Singleton

@Stateless

@LocalBean

 

 

 

 

public class LifeEventTimer{

          private static Logger logger = Logger.getLogger(LifeEventTimer.class);

 

 

 

          @Resource(mappedName="/ConnectionFactory")

          private ConnectionFactory connectionFactory;

 

          @Resource(mappedName="topic/Output")

          private Topic topic;

          private static String INTERNAL_LIFE_MESSAGE = "foo";

          private String USER_NAME="foo";

          private String PASSWD = "baz";

 

    /**

     * Default constructor.

     */

    public LifeEventTimer() {

    }

  

          public void send(final String messageText, String userName, String passwd) {

                    try {

                              Connection connection = connectionFactory.createConnection(userName, passwd);

                              logger.trace("connection: "+connection);

                              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                              logger.trace("session: "+session);

                              MessageProducer publisher = session.createProducer(topic);

                              logger.trace("publisher: "+publisher);

                              final TextMessage message = session.createTextMessage(messageText);

                              logger.debug("textMessage: " + message.getText());

                              publisher.send(message);

                              connection.close();

                    } catch (JMSException e) {

                              logger.error("JMSException: ", e);

                    }

          }  

 

 

          @Timeout

          @Schedule(second = "*/10", minute = "*", hour = "*", persistent = false)

          public void timeout() {

 

                    send(INTERNAL_LIFE_MESSAGE,USER_NAME, PASSWD);

 

          }

}

Reply to this message by going to Community

Start a new discussion in Beginner's Corner at Community