I want to create a class that will start with the glassfish server when it starts. I created a StartupListener class:
public class StartupListener implements LifecycleListener { public void handleEvent(LifecycleEvent event) throws ServerLifecycleException { if (LifecycleEvent.STARTUP_EVENT == event.getEventType()) { MessageReceiver receiver = new MessageReceiver(); } } } MessageReceiver - the class that will "listen" to the TestQueue and will display the text message that came there:
public class MessageReceiver implements MessageListener { /*@Resource(name = "MyTestConnectionFactory") ConnectionFactory factory; @Resource(name = "MyJMSTestQueue") Queue ioQueue;*/ MessageReceiver(){ Session session = null; Connection connection = null; ConnectionFactory factory = null; try{ Context jndiContext = new InitialContext(); factory = (ConnectionFactory)jndiContext.lookup("MyTestConnectionFactory"); Queue ioQueue = (Queue)jndiContext.lookup("MyJMSTestQueue"); connection = factory.createConnection(); connection.start(); session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(ioQueue); consumer.setMessageListener(this); System.out.println("Listening to the Test Queue.."); Thread.sleep(3000); }catch(JMSException e1){ System.out.println("Error: " + e1.getMessage()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("InterruptedException: " + e.getMessage()); } catch (NamingException e) { // TODO Auto-generated catch block System.out.println("NamingException: " + e.getMessage()); e.printStackTrace(); }finally{ try{ session.close(); connection.close(); }catch(Exception e){ System.out.println("Can't close JMS connection/session " + e.getMessage()); } } } @Override public void onMessage(Message arg0) { // TODO Auto-generated method stub String msgText; try{ if (arg0 instanceof TextMessage){ msgText = ((TextMessage) arg0).getText(); System.out.println("Got from the queue: " + msgText); }else{ System.out.println("Got a non-text message"); } } catch (JMSException e){ System.out.println("Error while consuming a message: " + e.getMessage()); } } } I created the lifecycle module using asadmin , and created a jar file in eclipse . Put it in the domain1/lib folder, but glassfish does not find it. I think the problem is that I incorrectly created the jar-file . Please tell StartupListener how to properly configure the jar file for the StartupListener class.
StartupListenername of the file with the source code of the classStartupListener.java. - Nofate ♦