aspose file tools*
The moose likes Java in General and the fly likes Loading a java class on server startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading a java class on server startup" Watch "Loading a java class on server startup" New topic
Author

Loading a java class on server startup

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

I need to call a java class on server startup. The java class/jar would be doing some activities like setting System Properties and stuff.

Is it possible to call a java class during startup. I came across java agents, have anybody used this kind of approach. I refer another post in coderanch on same topic Link.

It would be helpful if someone elaborates more.


Ashwin Sridhar
SCJP | SCWCD | OCA
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

When you say server do you mean the actual machine the program is running on, or the servlet container / application server to which the application is deployed?

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Application server where my application is deployed. I use Glassfish as app server
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Well it sort of depends on what you want/need to do exactly.
For instance you could use a load-on-startup Servlet, or an @Startup annotated EJB to execute some initialization / configuration stuff, but one might be more suitable than the other.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Thanks for your reply.

I want to configure my Jdbc-Resources on server level. I intend to read my connection details from a property file and set it on the system properties during startup.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Oh, in that case I wouldn't take this route at all.
You should probably look at the DataSource class and tutorial and how to configure one on GlassFish.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Thanks for your efforts.

I have a work around where I have property-placeholders in my domain.xml for Jdbc-resource-pool and I set the values on System-property on the admin console.

So, it becomes a two-step process. My trying to link some java class on my server startup, which would eventually set the values.

So my end-user would be unaware of the change.

Any thoughts on this.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Ashwin Sridhar wrote:
So my end-user would be unaware of the change.


This I don't quite follow. Are you trying to make your DataSource configurations editable at runtime?
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I was configuring my resources on the server. I used javaagent to load a class at start up and worked fine.

Thanks for your response.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading a java class on server startup