Best way to program a server status feature - java

Some background information.
- Running a java server on localhost
- Running a webserver on localhost
I would like a webpage to have a 'server status' feature which lets me know whether the server is running or not. My question, what is the best way to do this?
When I launch the java server, I write a flag in the database to signify that it is running.
Javascript/PHP sockets to try and bind on the same port. (Not sure if possible yet)
Shell script to locate the program in the task list.
Thanks!

When I launch the java server, I write
a flag in the database to signify that
it is running.
would not be of much help if the server should segfault.
Maybe have a look at http://mmonit.com/monit/
what is pretty much what you are looking for

I suspect the simplest method is simply for your web service (backend) to try and connect to the port that your server is running on, and provide an automatically refreshing page that reports this status. If your server goes down then you'll get an faster notification than if you're polling (say) the process table.
Of course the fact that you can connect to the port doesn't really give you an indication of whether it's working other than it's opened a port (e.g. it may have no resources etc. to service requests) but it's a start.

Related

How to launch an DataStage job ETL from Java client?

I premise that I don't know a lot about DataStage.
I just know that somebody else has created an "ETL job" and I need to launch it from my Java program, that acts as a client.
How should I do?
Edit:
The DataStage server is phisically different from the client where the Java program runs. I am not allowed running commans on the DataStage server. I need to connect via network. I imagined that DataStage provides some kind of network protocol, or webservice, or something so.
You can use the dsjob command - details see the Knowledge Center
Edit:
dsjob needs to be executed at the server.
You could use a insert into a database table from remote and trigger something (i.e. a UDF) that executes the dsjob on the server.
Alternatively use the WaitForFile stage and transfera file to the server.

How can I determine if another local machine is alive?

Is it possible to make my local computer function as a gateway in Java? I need the other local machines to connect directly to my computer to see if they are alive or not.
You could run a Java server program on your desired PC and let it listen on a port. Then you could use other programs (browser, other Java programs etc.) to connect to this port, and send commands to be executed by the Java server program.
If you just want to see if the PC is turned on or not, I'd just use the ping command though. Or see this answer: How to do a true Java ping from Windows?
Surely it's the other way round? Surely you want to connect to the other machines to see if they're alive? In which case see InetAddress.isReachable().
Try this.
Create a Java Server Socket, which keeps listening to the client at some port.
Write a client in Java which connects to the Server, wrap the connection logic in try-catch block....
If your host is alive the try code is executed which contains the code to connect to the
Server, if this connection process fails you will get UnknownHostException, here you can instead type a message that the connection failed.
You could more easily manage and control this by polling for other devices from a central server. If possible, avoid unnecessary client/agent apps that might tax your development and support resources as well as taking up RAM on the client workstations.
There are many monitoring tools that already do what you want. I'd have a look at Nagios, for example.
If you want to develop your own app, do your own quick troubleshooting, or just get a feel for network discovery tools, then take a look at NMAP. You could, for example, search a subnet for anything that responds to TCP:445 and see what Windows machines are alive.
If you do go the Nmap route, please have a look at Nmap4j on Sourceforge. It's a Java wrapper API that simplifies the work needed to integrate Java and Nmap.
Cheers!

Can a Java applet function as a server?

Is it possible to write a Java applet that can be a server on the client machine within the client's local network?
To be more specific, what I am looking to do is tunnel non-web traffic over the web. The sender would send to the applet, which would then forward the received data back to the server.
Is this sort of thing possible? What are the restrictions that might get in the way?
Note: I know that the applet can connect back to the server, that isn't an issue. The issue is whether or not an applet can listen for a connection / data on a local, client-side port.
An unsigned applet can only connect the host they come from.
A signed applet can do any connection you want and can listen on tcp-ip ports.
Source : http://docs.oracle.com/javase/tutorial/deployment/applet/security.html
Server does not connect to anywhere. Server opens server socket and is listening.
In past as far as I remember the server socket was restricted in MSIE and was permitted in Netscape (do you remember such browser?) :)
I personally have not been writing applets for the last 10 years, so I have no idea what happens now with currently existing browsers, but it is very easy to check. Just write the shortest applet you can and put code new ServerSocket(1234).accept(); into its init() or start() method. If no exception was thrown you can write applet that functions as a server. Otherwise you cannot.
Just try it with all available browsers. 20 minutes work and you are done. Good luck. I'd will be glad to know about the results.
Generally, it cannot.
One reason why is that applets tend to be ran within security constrained environments, which means that they are denied the ability to open server sockets.
There are ways around such a restriction, basically you can specify a special security policy for the applet, or run it in a special unconstrained container; but why bother when you can just port the contents of your application into a standard servlet, or even a stand-alone server?

nhttp does not show the proper status

Many of the times our Domino http is making me tense.
Because of the various reasons, Please tell the proper solution for
this...
I started my both domino and Notes. After sometime I restarted my domino server. Now my nhttp is not getting started on my server. It is telling http is already running. But I have checked my Task manager and found that port 80 is occupied by nhttp. After I have end that process it is again restarted. Why is this happening?
Some of java programs run on server. When the JVM may crashed. That time I have no choice except restarting the server. Pleae tell is their any way to restart JVM. or Kill the nhttp.
Make sure the IIS-service isn't started, the HTTP-task will take and hold port 80.

How to shutdown com.sun.net.httpserver.HttpServer?

The Http Server embedded in JDK 6 is a big help developing web services, but I've got situation where I published an Endpoint and then the code crashed and left the server running.
How do you shutdown the embedded server once you've lost the reference to it (or the published Endpoint)?
I use the below code to start it
this.httpServer = HttpServer.create(addr, 0);
HttpContext context = this.httpServer.createContext("/", new DocumentProcessHandler());
this.httpThreadPool = Executors.newFixedThreadPool(this.noOfThreads);
this.httpServer.setExecutor(this.httpThreadPool);
this.httpServer.start();
and below code to stop it
this.httpServer.stop(1);
this.httpThreadPool.shutdownNow();
I've never used this server before and I can't find any good documentation. Perhaps these less elegant solutions have occurred to you already, but I just thought I would mention them.
Seems like com.sun.net.httpserver.HttpServer has an implementation class called HttpServerImpl. It has a method called stop().
Or perhaps you can find the Thread listening on the server socket and call interrupt().
Sean
How about not loosing the reference, then? When you say your code crashes, I assume you get an exception somewhere. Where exactly? So someone capable of intercepting this exception obviously needs to also have a reference to the HttpServer, which you might have to pass around yourself.
Edit: Oh. In that case if you don't want to kill the entire JVM with the HttpServer in it, then you will need to offer some form of IPC to the environment, e.g. a command channel via RMI that can be invoked from a Java program (and hence Ant).
Another solution would be to have the server listen for some "secret" cookie query, where you e.g. print/save the cookie on startup so that the Ant script can retrieve the cookie, and you can fire off a query to your "secret" URL upon which the server will exit itself gracefully.
I'd go with a quick RMI solution.
netstat -a
to find the pid of the process that has the port open (assuming you know the port), and
kill -9 $pid
to kill the process.

Categories