I have a console Java application, which runs all the time on a remote server (nohup java -jar myapp.jar &).
Frequently (every other day) I need to replace the JAR file by a newer one (whenever I fix a defect).
At the moment I do this by connecting to the server using ssh (WinSCP).
How can I create a mechanism, using which I could upgrade the application via the http protocol? Can I use JMX for that?
That mechanism should do following things:
a) Stop the currently running application.
b) Upload the new JAR file to the server.
c) Launch the new JAR file.
Basically you need to break down the program into two parts:
the monitor component which fetches the new jar, stops the program, replaces the jar, restarts the program
the actual program, the console java application
Theoretically you can do all of this in a single java process but the additional complexity is not worth the trouble in my opinion.
You might check out install4j or this similar StackOverflow question.
In LiveRebel, there's native support for standalone applications (i.e. daemons). So the main application is running with the watchdog agent which communicates to the command center. From command center it is possible then to manage the updates.
Related
I'm writing a program in java and I was curious as to whether there was any command or code that could be added to make it always run on startup. For example once someone downloads the program it runs whenever they log into their computer. I'm not sure if this matters but I'm writing it in the Netbeans IDE.
You could give your application an option (user can choose it) that will create an entry in the startup/run-once part of the operating system. On windows you could write to the registry to set the option (maybe program need higher priv. to do that) or you could put a link to the startup folder. This might depend on the operating system.
Try Java Service Wrapper
http://wrapper.tanukisoftware.com/doc/english/introduction.html
This lets you run a Java application as a Windows Service or UNIX Daemon.
i have deployed a java server using my eclipse.
I extract the war file.
i installed the apache-tomcat-7.0.47 on my windows server 2003
i installed the Apache Tomcat 7 service on my windows server 2003 and made it run automatically.
i want to run the war file on my windows server 2003
what i have tried
i put the war file on my webapps on the apache-tomcat folder and then run the startup.bat which locates on the bin folder.
i test the server and it works perfectly
my problem
when i log of from my windows server. the war file stop working.
my question
how can i keep the server working ever when i log of. note that i installed the service and restart the server many times.
One way to do this is to use a Java Server Wrapper or http://support.microsoft.com/kb/137890
SO Link
There seems to be a way in java itself to do this, add -Xrs to your java.exe call in server startup (bat file I suppose), from Oracle Documentation . Beware of the consequences in using this!
-Xrs Reduces use of operating-system signals by the Java VM.
In an earlier release, the Shutdown Hooks facility was added to enable
orderly shutdown of a Java application. The intent was to enable user
cleanup code (such as closing database connections) to run at
shutdown, even if the Java VM terminates abruptly.
The Java VM watches for console control events to implement shutdown
hooks for unexpected Java VM termination. Specifically, the Java VM
registers a console control handler which begins shutdown-hook
processing and returns TRUE for CTRL_C_EVENT, CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT.
The JVM uses a similar mechanism to implement the feature of dumping
thread stacks for debugging purposes. The JVM uses CTRL_BREAK_EVENT to
perform thread dumps.
If the Java VM is run as a service (for example, the servlet engine
for a web server), then it can receive CTRL_LOGOFF_EVENT but should
not initiate shutdown because the operating system will not actually
terminate the process. To avoid possible interference such as this,
the -Xrs command-line option was added beginning with J2SE 1.3.1. When
the -Xrs option is used on the Java VM, the Java VM does not install a
console control handler, implying that it does not watch for or
process CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, or
CTRL_SHUTDOWN_EVENT.
There are two consequences of specifying -Xrs:
Ctrl-Break thread dumps are not available.
User code is responsible for causing shutdown hooks to run, for
example by calling System.exit() when the Java VM is to be terminated.
After reading aksappy's answer. I discovered that the jvm is making that problem. i went to the bin folder of the apache-tomcat and run the tomcat7w.exe and then I went to the shutdown tab and changed the jvm to java.
this is the facinate solution that helped me
You can set the “deployIgnore” attribute for your web application in server.xml ; this attribute will ignore the war file name from deployment. Later you can deployment the application manually
I have a Stand-alone Java application. At the moment I am running this Java application using a start-script i.e. startApplicatoin.bat in windows and startApplicatoin.sh in Linux which sets up the class-paths and then it executes: java -classpath .
Now I have to add a stopApplication.bat and stopApplication.sh script. This stop script has to shutdown/close this java application gracefully.
To achieve this I am planning to take the following steps:
1. When my java application runs it will store the process-id of the launched application in a file i.e. in a known file myapplication.pid.
Looks like ManagementFactory.getRuntimeMXBean().getName() call will work on both Linux and Windows to get the process ID. So I shall collect process ID in this way and will store it in the specified file myapplication.pid.
2. Then when running stop application script, this script will issue a “kill” request to the process-id as specified by that myapplication.pid file.
For Windows I shall run the "taskkill" command to stop this application. And for Linux environment "kill" command will serve that purpose.
And in my java code I shall add a addShutdownHook which will enable the graceful shutdown operations that I want to run i.e. there I shall handle whatever stuffs I want to persist before this program is going to stop.
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29
Now I would like to do a sanity check to ensure the way I am thinking is the proper way to do. Or there is a better way to do this. Any suggestion is appreciated. And thanks in advance.
If you're wanting a "graceful" shutdown, it may be more practical (and easier cross-platform) to open a socket in your long-running process and have your "stop" script connect to it and issue a shutdown command; this might even be practical through JMX, depending on how your application overall is structured. Approaches that are "inline" rather than requiring interaction with the OS are generally easier to reason about and test.
This looks like a Daemon.
The easiest way to run a daemon with start/stop functionality without resorting to a lot of scripting is with jsvc. This allows your code to implement an interface with four methods:
void init(String[] arguments): Here open configuration files, create a trace file, create ServerSockets, Threads
void start(): Start the Thread, accept incoming connections
void stop(): Inform the Thread to terminate the run(), close the ServerSockets
void destroy(): Destroy any object created in init()
You then have platform specific binaries that deal with keeping track of the process and stopping it when requested to do so.
The most useful thing is that jsvc can start a process as a superuser (root on unix) and then drop to a peon user the for auction running of the process.
This is how Tomcat (for example) works, it starts as root and performs privileged actions such as binding to port 80. It then drops down to a peon use called tomcat for security reasons.
We Have a Multi-threaded Application in JAVA which has multiple threads running in parallel. Now we want to run all these threads on a single core. Currently application is running on a system having more then one Cores.
We know there is a technique available ProcesAffinity in .Net Framework to set process affinity.
But we don't want to depend on .Net Framework, because our application is build in java.
Do we set Process affinity using Bat file and run our application executable jar file through Bat file?
Currently our application is running on Window XP. So we need a solution that should be working fine on XP platform.
EDIT:
It's possible: See Java thread affinity
Pure Java doesn't support running a thread on specific processor. Check the SO question linked above.
Personally, I don't think that the fact that this cannot be set in pure Java is a bad thing, as to me, how an app is run does very much depend on the OS, so therefore a OS-specific solution isn't a bad thing.
You can use the MS psexec utility to set the affinity:
psexec -a 1 java -jar myapplication.jar
Would instruct that all of the threads created by java would be run on the lowest CPU.
And this line would be your .BAT file...
You cannot do it in pure Java. But on some versions of Windows, you can do it via operating system utilities; see https://superuser.com/questions/309617/how-to-limit-a-process-to-a-single-cpu-core ... and you might be able to do this by calling native libraries via JNI.
I'm trying to deploy my Java application using Java Web Start, but I've come across a problem. When running the application through my IDE (Netbeans) it's lightning fast; everything happens within seconds. However if it's launched with Web Start it's excruciatingly slow.
Any ideas why?
I'm self-signing everything and using full permissions in the JNLP.
Java Web Start applications will always launch more slowly than launching directly via the java executable. This is because Java Web Start is doing so much more than a standard java launch:
JWS accesses the network to download the JNLP (and possibly JARs, and other resources)
JWS is checking its local cache of JARs to see if it can skip downloading the JARs
JWS is potentially downloading the jars via HTTP, if the cache is empty or out of date
JWS is checking the validity of the cryptographically strong signatures of every single jar
I think most of this is an unavoidable cost for the added power of being able to launch your application via a web browser and update your application virtually "for free."
Another possibility: Is your application running with a different VM when you launch via webstart vs. when you launch with NetBeans? You can try printing:
System.getProperty("java.home")
to find out what VM is running the currently executing process.
If it's the same VM, you might also try diffing the Java system properties between the two scenarios. Try printing out all System properties:
System.getProperties().store(System.out, "");
Wireshark can be used to help diagnose Java Web Start network issues.
Sometimes the Java Console and Java Web Start Logging can be useful in diagnosing certain JWS problems. You can enable these features through the Java Control Panel:
You can enable full logging in the Java Console and see all the things that Java Web Start is printing of debug information. You can then see where the pauses happen, which may give you an indication of the problem.
My initial guess would be that you have DNS issues, especially if the pauses are very close to a multiplum of 30 seconds.