0

I have the following two questions:

  1. How to start a java class (not a servlet or JavaBean) on GlassFish 4 server via Eclipse Juno or otherwise?

  2. What is the proper way to run it on start-up?

I have Java class that uses JDNI functionality and needs to run on the server. How it can be done?

I found some information about running it on server start-up, which boiled down to the following options:

  • using both @Startup and @Singleton annotations (which, as I understand, are specifically for EJBs)
  • implementing LifecycleListener interface
  • implementing Startup interface

Oracle GlassFish Server Application Development Guide clearly states:

Lifecycle listener modules are deprecated. Support for them is included for backward compatibility. Implementing the org.glassfish.api.Startup interface instead is recommended.

When I added org.glassfish.api.* import and had my class to implement Startup interface Eclipse warned me that it was deprecated. So I'm not sure what's left there since my class is not EJB. I went ahead with Startup interface anyway and to satisfy its it requirements added the following method:

@Override
public Lifecycle getLifecycle() {
    return Lifecycle.SERVER;
}

In Eclipse I stopped and started the server but the class seemingly did not run. My class has System.out.println() call but I did not see its message either in Server and Console tabs nor in the server log.

So this is were I stuck.

Would appreciate your suggestions.

CLARIFICATION:

My start-up related part of the questions refers to server (i.e. GlassFish) start-up (and not a web application start-up). I need to force GlassFish to instantiate this class when the server starts and let it run for the duration of the server life.

It is basically a Message Listener that need to be up to monitor a queue and respond to request from various senders.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • Is it a problem your class becoming an EJB? There is usually nothing that will do it harm. – rdcrng Aug 15 '13 at 17:49
  • @rdcrng - This task is from the book I'm using and comes before the chapter on EJBs. So I accepted it as a challenge but got stuck. – PM 77-1 Aug 15 '13 at 17:53
  • Well, on a server, anything should be invoked by something else - such as servlets, web services, EJBs, timers (which are tied to EJBs), JMS, etc. I don't think you can get around using some of that. – rdcrng Aug 15 '13 at 17:57
  • @rdcrng - I'm under impression that there should be a way to tell GlassFish to load a certain *plain* class on server start-up. Such *instruction* could be in annotation, interface, XML or any combination of the three. – PM 77-1 Aug 15 '13 at 20:38
  • If it does exist, I'm not aware of it _shrug_ – rdcrng Aug 15 '13 at 20:39
  • See http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html – JB Nizet Aug 15 '13 at 21:01
  • @JBNizet - I must be thick today. How does your link apply to my question? – PM 77-1 Aug 15 '13 at 21:59
  • 1
    You want something to happen at startup of your application, and this is how you can define a listener which is called when the application starts up (i.e. is deployed). The javadoc of contextInitialized() is: "Receives notification that the web application initialization process is starting.". So you define a listener, implement this method, and do whatever you want at startup. – JB Nizet Aug 16 '13 at 06:51
  • @JBNizet I want something to happen when a **server** starts up (not an application). – PM 77-1 Aug 16 '13 at 18:48
  • If you deploy an application on Glassfish, it will start when the server starts, and stop when the server stops. That's the whole point of an application server: run server-side applications. – JB Nizet Aug 16 '13 at 19:22
  • @JBNizet - May be we differ in terminology. I currently have 5 web applications deployed on the server. None of them runs until I explicitly initiate them. I mean that they do not start performing their tasks **automatically** after `asadmin start-domain` (or its Eclipse equivalent) is completed. – PM 77-1 Aug 16 '13 at 19:45
  • What do you mean by "initiate"? Are you saying that you must redeploy the application each time you restart the server? I have 0 experience with Glassfish, but I would be very surprised if you had to do that. What task are you talking about in "they do not start performing their tasks"? – JB Nizet Aug 16 '13 at 20:05
  • GlassFish do startup deployed applications on startup. Well kinda. Application Clients does not. For them to be able to launch, one has to redeploy the entire GlasFish application. – Martin Andersson Aug 17 '13 at 06:47
  • PM.. what book are you reading? I'm curious. I want challenges too! – Martin Andersson Aug 20 '13 at 18:31
  • Is there a particular reason why you can't make your message listener class a message-driven EJB? That's what message-driven beans are designed to do. – Ian Evans Aug 20 '13 at 18:46
  • @ Martin Andersson - I read a book that was written using `Java 6` and `GlassFish 3.x`, but I decided to install `Java `7 and `GlassFish 4`. So I have to do minimal adaptation. Here it is: [http://www.wrox.com/WileyCDA/WroxTitle/Java-Programming-24-Hour-Trainer.productCd-0470889640.html]. Look at reviews on Amazon and believe those that say that there a lot of typos and some errors, while errata is not complete (to say the least). – PM 77-1 Aug 20 '13 at 18:50
  • @IanEvans - As I have explained way above, `EJB`s (including `MDB`s) are introduced in the next chapter. So, I assumed there should be away to accomplish the same without that. On the other hand, the point that my class is not an `servlet` is actually a mute one, since I can always create a bare-bone one that instantiates the class. – PM 77-1 Aug 20 '13 at 18:57
  • @PM77-1 - Is your listener using JNI ("Java Native Interface", i.e. native applications written in some other language), or JNDI ("Java Naming and Directory Interface", i.e. a way of referring to and looking up resources and components in a standard way)? I also think you'll find it much easier to accomplish your goals if you start out using the correct technology, and not depending on the learning path of a book. The point of, e.g., Java EE is to simplify network application development. Trying to recreate the same features in Java SE first is extremely difficult. – Ian Evans Aug 20 '13 at 21:53

0 Answers0