Skip to main content

restart glassfishv2u2 programmatically

25 replies [Last post]
montsie
Offline
Joined: 2008-07-15
Points: 0

Hi

Is there a way to restart glassfish programmatically?

Or how can I restart glassfish from an application which runs in that glassfish?

I'm using glassfishv2u2 and WindowsXP.

Thanx

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anzy
Offline
Joined: 2008-09-17
Points: 0

Is your domain healthy now ?
Do you face the same issue when you manually stop and start the domain ?
Pls check and revert.

Also let me know if you are running webserver along with glassfish
Regards
Anzy

montsie
Offline
Joined: 2008-07-15
Points: 0

>Do you face the same issue when you manually stop and start the domain ?

No. If I execute my restartgf.bat manually then it works fine.

But If I use the the restart feature from our administration client then it stops
the server (or at least the log says that the server is stopped).

If I uses now "netstat -a" I see that:

TCP lzgkam2:1625 localhost:8080 TIME_WAIT
TCP lzgkam2:1626 localhost:8080 TIME_WAIT
TCP lzgkam2:1628 localhost:8080 TIME_WAIT
TCP lzgkam2:1629 localhost:8080 TIME_WAIT
TCP lzgkam2:1630 localhost:8080 TIME_WAIT
TCP lzgkam2:8080 localhost:1627 TIME_WAIT

Why is the port 8080 state TIME_WAIT? And the others?

Now after 30sec the application server should "re start" but then a port conflict occurs. And the server stops...

If I uses now "netstat -a" I see that no 8080 port are active or waiting...
Now is the application server correctly stopped.

In the log I see no difference between a manually restart and a programmatically.

>Also let me know if you are running webserver along with glassfish?

No.

Any Suggestion?
Thanx

anzy
Offline
Joined: 2008-09-17
Points: 0

Time_wait does not cause conflict. After some time if you check again you'll see all you time_waits have disappeared. In plain language it is one of the state of shutting down the port.

In your script of restart, precede shutdown with start

echo start domain....
start D:\dev\server\bin\startgf.bat
echo stop domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
exit

use :
echo stop domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
echo start domain....
start D:\dev\server\bin\startgf.bat

:)

montsie
Offline
Joined: 2008-07-15
Points: 0

>Time_wait does not cause conflict. After some time if you check again you'll see all you time_waits have disappeared. In plain language it is one of the state of shutting down the port.

Sorry my fault the port 8080 was state listening...

If I uses "netstat -a"I see something like:

TCP lzgkam2:1625 localhost:8080 TIME_WAIT
TCP lzgkam2:1626 localhost:8080 TIME_WAIT
TCP lzgkam2:1628 localhost:8080 TIME_WAIT
TCP lzgkam2:1629 localhost:8080 TIME_WAIT
TCP lzgkam2:1630 localhost:8080 TIME_WAIT
TCP lzgkam2:8080 localhost:1627 LISTENING

>In your script of restart, precede shutdown with start

> use :
> echo stop domain....
> call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
> echo start domain....
> start D:\dev\server\bin\startgf.bat

If I use your example manually or programmatically the server is stopped but never "re started" because on server stop the batch will not continue and the line "start D:\dev\server\bin\startgf.bat" is never called..

I think my script is ok because it works manually fine and programmatically it seams to work fine ((see log previous post). But when I do it programmatically I see that port 8080 is state Listening until the restart cause the server to stopped because a port conflict has occured(see log previous post).

So the programmatically way stopps the server not 100% the port 8080 is still listening. Why?

Any Idea?
Thanx

anzy
Offline
Joined: 2008-09-17
Points: 0

Can you please explain how running of batch is dependent on running of domain ?
A batch file will run even if the domain stops.
To restart an instance you need to first stop it and then start it again.
I am assuming that you run your restart batch file when domain is alive, i.e it is listening to 8080
Hence, when you run restart.bat it fails to start because port 8080 is already listening.

You may want to modify your restart script as I am still not convinced with the
starting an already up domain.

The restart script should be something like this:

stop-domain
wait - for 5 mins
start-domain

Please do one thing : If your domain is stopped then start it manually.

Then try manually what you have mentioned in your script - stepwise
.
start D:\dev\server\bin\startgf.bat
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain

You'll see the difference

montsie
Offline
Joined: 2008-07-15
Points: 0

> Can you please explain how running of batch is dependent on running of domain ?

I can't.

>You may want to modify your restart script as I am still not convinced with the
starting an already up domain.

>The restart script should be something like this:

>stop-domain
>wait - for 5 mins
>start-domain

Please have a look at my script then you see it does exactly that(expect that it sleeps only for 30sec)...

restartgf.bat:
@echo on
echo start domain (in new cmd)....
start D:\dev\server\bin\startgf.bat
echo stop domain (in same cmd)....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
exit

startgf.bat:
@echo on
echo wait ...
rem wait 30sec
@ping 127.0.0.1 -n 2 -w 1000
@ping 127.0.0.1 -n 30 -w 1000
echo start domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain
exit

>Please do one thing : If your domain is stopped then start it manually.

>Then try manually what you have mentioned in your script - stepwise
.
>start D:\dev\server\bin\startgf.bat
>call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain

Ok. I have done that. No exceptions everything fine. The server stops and after 30sec the server re-starts... My scripts are fine!

>You'll see the difference

I see manually it works and programmatically it fails....

Like I explained in previouse posts...

Note: I try to migrate from Tomcat to Glassfish. With Tomcat I had no problems to restart using batch files...

Impossible with glassfish? (The admin console has no restart funktion too. Only a stop function)

Any Idea? Any suggestion?
Thanx

anzy
Offline
Joined: 2008-09-17
Points: 0

For my sake try the following once :

Make sure the domain is running before you run the script, you can check this by following command:
netstat -na | grep 4849
if you get a +ve result that means domain is up.

restartgf.bat:
@echo on
echo stop domain (in same cmd)....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
echo wait ...
rem wait 60sec
echo starting in 4 mins...
rem wait 60sec
echo starting in 3 mins...
rem wait 60sec
echo starting in 2 mins...
rem wait 60sec
echo starting in 1 min...
rem wait 60sec
echo starting now...
@ping 127.0.0.1 -n 2 -w 1000
@ping 127.0.0.1 -n 30 -w 1000
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain
exit

Now call restartgf.bat and let me know what happens. I have intentionally introduces the 5 mins wait period.

montsie
Offline
Joined: 2008-07-15
Points: 0

>Make sure the domain is running before you run the script, you can check this >by following command:
>netstat -na | grep 4849
>if you get a +ve result that means domain is up.

WindowsXP:
'grep' is not recognized as an internal or external command,
operable program or batch file.

I connect with my Administration Client to the Server to verify that the Server (Domain) is running. Ok.

> Now call restartgf.bat and let me know what happens. I have intentionally introduces the 5 mins wait period.

manually:::::

works fine.

programmatically::::

stops the server but can't restart it because a port conflict occured.

(for output log see attachment)

:(

anzy
Offline
Joined: 2008-09-17
Points: 0

what is the difference b/w manually and programmatically when you have run the script for both.
Second why is there no gap of 5 mins between server shutdown and start up when we had introduced it in the restart script i had copied to you in the last post

montsie
Offline
Joined: 2008-07-15
Points: 0

Hi anz

> what is the difference b/w manually and programmatically when you have run the script for both.

Again manuall it works and programmatically it "stops" the server but the server is still listening on port 8080 (netstat -a) and then on "re start" a port conflict occurs!

(see my second post in this thread)

>Second why is there no gap of 5 mins between server shutdown and start up >when we had introduced it in the restart script i had copied to you in the last post

Do you understand dos batch files and commands?

rem wait 60sec // is just a comment
echo starting in 4 mins... // prints "starting in 4mins..." to the console
@ping 127.0.0.1 -n 30 -w 1000 // does wait 30 sec

So.. have a look at the script that you have posted!

anzy
Offline
Joined: 2008-09-17
Points: 0

oops my fault..sorry
but when i asked what is the difference b/w manually and programmatically when you have run the script for both i meant how do they differ in terms of implimentation.
I understand that when you stop the server manually through asadmin and then restart it manually through asadmin no problem occurs. But when you use the script which I posted, the domain stops but the same script is unable to start it because of some port conflict.

Can you please modify my script and introduce a timegap of 5 mins instead of 30 secs and then tryagain

Sorry to have wasted your time ..

Regards
Anzy

montsie
Offline
Joined: 2008-07-15
Points: 0

>but when i asked what is the difference b/w manually and programmatically when you have run the script for both i meant how do they differ in terms of implimentation.

Programmatically:

I use my Administration Client to call my restart jax-rpc webservice method(in that glassfish that should be restarted) which calls the Restarter.class and the Restarter calls the restartgf.bat.

public class Restarter extends Thread {
public void run() {
try {
Process p = Runtime.getRuntime().exec("d:/dev/server/bin/restartgf.bat");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println("RESTARTER:" + line);
}
input.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}

>Can you please modify my script and introduce a timegap of 5 mins instead of 30 secs and then tryagain

The same. The port 8080 is listening (5min)until "re start" cause the server to stop (because of the port conflict).

Thanx

anzy
Offline
Joined: 2008-09-17
Points: 0

I am getting your concern now, loud and clear..thanks for the explanation

When the server stops is the administrative port(4848) also listening like port (8080) ?
In you script can you echo the output of netstat -a between shutdown and startup and check the output ?

And I know it would be an untidy advice but you can also kill the process which is using the 8080 port (which most likely would be your application which you use to restart domain) in a parallel thread after starting the domain programmatically.

montsie
Offline
Joined: 2008-07-15
Points: 0

>When the server stops is the administrative port(4848) also listening like port (8080) ?

Yes. see the attachment for details...

And I see durring this 5min wait period:
- i can delete the domain server.log file but not the jvm.log file.
- if I try to connect to the server with my Administration client it blocks and after 5min an exception occurs: server not available...

Note: The same scenario works on tomcat! :(

>And I know it would be an untidy advice but you can also kill the process which is using the 8080 port (which most likely would be your application which you use to restart domain) in a parallel thread after starting the domain programmatically.

If i have time... I try that tomorrow

Any other input?
Thanx

montsie
Offline
Joined: 2008-07-15
Points: 0

I found a solution (it's more a workaround).

When I start glassfish as an application then I start a gfdaemon.bat file too. Which trys in a loop to start the glassfish server and then waits 60sec.

gfdaemon.bat:

:loop
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain
rem wait 60sec
ping 127.0.0.1 -n 60 -w 1000
goto loop

Now I use my Administration Client to call my restart jax-rpc webservice method(in that glassfish that should be restarted) which calls the Restarter.class and the Restarter calls the stopgf.bat.

stopgf.bat:

echo stop domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
exit

It works fine expect that the daemon.bat needs 25% of my cpu. :(

But at least I can restart the server from my Administration Client.

Stephen Connolly

I'm sure you could write a simple java program that would check if your
glassfish is up: a simple get of http://localhost:4848 should do... if it
times out - then it's dead!

if it detects that it's dead, then fork an cmd
D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain

in any case, sleep for 60 seconds before looping again

it would be much tighter than the batch file in terms of CPU

On 19 September 2008 11:11, wrote:

> I found a solution (it's more a workaround).
>
> When I start glassfish as an application then I start a gfdaemon.bat file
> too. Which trys in a loop to start the glassfish server and then waits
> 60sec.
>
> gfdaemon.bat:
>
> :loop
> rem echo start domain....
> call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain
> rem wait 60sec
> echo wait 60sec...
> ping 127.0.0.1 -n 60 -w 1000
> goto loop
>
> Now I use my Administration Client to call my restart jax-rpc webservice
> method(in that glassfish that should be restarted) which calls the
> Restarter.class and the Restarter calls the stopgf.bat.
>
> stopgf.bat:
>
> echo stop domain....
> call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
> exit
>
>
> It works fine expect that the daemon.bat needs 25% of my cpu. :(
>
> But at least I can restart the server from my Administration Client.
> [Message sent by forum member 'montsie' (montsie)]
>
> http://forums.java.net/jive/thread.jspa?messageID=300340
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>
[att1.html]

wfay
Offline
Joined: 2006-05-05
Points: 0

I have to agree entirely with Stephen. The 25% of your CPU is due to executing the ping command.

If there is no sleep() command in DOS/Windows batch file scripting, then you really should write a little Java (or C, or Perl, or anything with a proper sleep()) and use that instead.

montsie
Offline
Joined: 2008-07-15
Points: 0

>If there is no sleep() command in DOS/Windows batch file scripting, then you >really should write a little Java (or C, or Perl, or anything with a proper sleep()) >and use that instead.

I wrote a small Java Programm... It needs 1% of my cpu. :)

Thanx

anzy
Offline
Joined: 2008-09-17
Points: 0

if it the whole domain you want to restart and you are sure about what you want to do then as far as I know you can achieve it by including a small script in your application and interfacing it wherever required.

montsie
Offline
Joined: 2008-07-15
Points: 0

Thanx for the answers...

I have written to batch files to try anzy's approche...

restartgf.bat:

@echo on
echo start domain....
start D:\dev\server\bin\startgf.bat
echo stop domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat stop-domain
exit

startgf.bat:

@echo on
echo wait ...
rem wait 30sec
@ping 127.0.0.1 -n 2 -w 1000
@ping 127.0.0.1 -n 30 -w 1000
echo start domain....
call D:\dev\3rdparty\glassfish-v2ur2\glassfish\bin\asadmin.bat start-domain
exit

To start the restartgf.bat I use:

Runtime.getRuntime().exec("D:/dev/server/bin/restartgf.bat");
...

But the server isn't stopped 100% (the log says something else...) and then on "restart" a port conflict occurs...

server.log (stop-domain):

[#|2008-09-17T16:27:33.593+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|CORE5051: Shutting down all J2EE applications ...|#]

[#|2008-09-17T16:27:33.593+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|CORE 5060 : Stopping all J2EE Connector 1.5 Compliant resource adapters ...|#]

[#|2008-09-17T16:27:33.603+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|CORE 5061 : Resource adapters Stop() Complete|#]

[#|2008-09-17T16:27:33.603+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|CORE5052: Application shutdown complete.|#]

[#|2008-09-17T16:27:33.693+0200|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|[AutoDeploy] Disabling AutoDeployment service.|#]

[#|2008-09-17T16:27:34.093+0200|INFO|sun-appserver9.1|com.sun.jbi.framework|_ThreadID=14;_ThreadName=RMI TCP Connection(6)-127.0.0.1;|JBIFW0042: JBI framework termination complete.|#]

30sec later...

server.log (start-domain):

[#|2008-09-17T16:28:01.893+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=10;_ThreadName=main;|Starting Sun Java System Application Server 9.1_02 (build b04-fcs) ...|#]

[#|2008-09-17T16:28:02.044+0200|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=11;_ThreadName=pool-1-thread-2;com.sun.enterprise.interceptor.DynamicInterceptor;|MBeanServer started: com.sun.enterprise.interceptor.DynamicInterceptor|#]

[#|2008-09-17T16:28:03.305+0200|SEVERE|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=10;_ThreadName=main;8080;|A conflict occured on port 8080 . Server is shutting down.|#]

Any Suggestion?

wfay
Offline
Joined: 2006-05-05
Points: 0

There are very few good reasons to do this. What problem are you trying to solve?

One solution might involve an external cron job that looks for the presence of a file located in your webapp's temp directory. When you need things restarted, you create the file, cron sees it, deletes the file, then kicks asadmin with the proper parameters to restart things.

montsie
Offline
Joined: 2008-07-15
Points: 0

Hi wfay

> There are very few good reasons to do this. What problem are you trying to solve?

We have an administration client (jax-rpc) which has a function to patch parts of our application. Patch means in our case: Copy new classes to - and override existing class in the application server shared classes folder. To "load" the overriden classes we "need" an application server restart.

I need a Solution to do this under MS Windows.

Thanx

wfay
Offline
Joined: 2006-05-05
Points: 0

To use cron, you would need to install Cygwin since you are on Windows.

Alternatively, Windows has a similar service "at":

C:\>at -?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.

I would use the technique I suggested (existence of a file causes a script run by "at" every 5 minutes to trigger a restart) since it seems like your other approach is not working.

wabunoh
Offline
Joined: 2003-06-11
Points: 0

That would be a security problem. Not sure this would be allowed. A single glassfish container may host many applications. To allow a single application, other than the console, to resart the whole container would be a security nightmere.

If you must do it, I would say you would need to do it through JMX and the Managed Beans.

montsie
Offline
Joined: 2008-07-15
Points: 0

Hi wabunoh

> If you must do it, I would say you would need to do it through JMX and the Managed Beans.

Impossible because there is no restart method(or I found no one). I found only a stop- and start function. (If I stop the server over jmx then i can't "re start" it again because the jmx service is stopped too.)

Any Suggestion?
Thanx