I have the following two questions:
How to start a java class (not a servlet or JavaBean) on
GlassFish 4
server via Eclipse Juno or otherwise?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.