views:

166

answers:

3

Hi

I'm trying to develop an application that just before quit has to run a new daemon process to execute the main method of a class.

I require that after the main application quits the daemon process must still be in execution.

It is a Java Stored Procedure running on Oracle DB so I can't use Runtime.exec because I can't locate the java class from the Operating System Shell because it's defined in database structures instead of file system files.

In particular the desired behavior should be that during a remote database session I should be able to

call the first java method that runs the daemon process and quits leaving the daemon process in execution state

and then (having the daemon process up and the session control, because the last call terminated) consequentially

call a method that communicates with the daemon process (that finally quits at the end of the communication)

Is this possible?

Thanks

Update

My exact need is to create and load (reaching the best performances) a big text file into the database supposing that the host doesn't have file transfer services from a Java JDK6 client application connecting to Oracle 11gR1 DB using JDBC-11G oci driver.

I already developed a working solution by calling a procedure that stores into a file the LOB(large database object) given as input, but such a method uses too many intermediate structures that I want to avoid.

So I thought about creating a ServerSocket on the DB with a first call and later connect to it and establish the data transfer with a direct and fast communication.

The problem I encountered comes out because the java procedure that creates the ServerSocket can't quit and leave an executing Thread/Process listening on that Socket and the client, to be sure that the ServerSocket has been created, can't run a separate Thread to handle the rest of the job.

Hope to be clear

+1  A: 

I'd be surprised if this was possible. In effect you'd be able to saturate the DB Server machine with an indefinite number of daemon processes.

If such a thing is possible the technique is likely to be Oracle-specific.

Perhaps you could achieve your desired effect using database triggers, or other such event driven Database capabilities.

I'd recommend explaining the exact problem you are trying to solve, why do you need a daemon? My instict is that trying to manage your daemon's life is going to get horribly complex. You may well need to deal with problems such as preventing two instances being launched, unexpected termination of the daemon, taking daemon down when maintenance is needed. This sort of stuff can get really messy.

If, for example, you want to run some Java code every hour then almost certanly there are simpler ways to achieve that effect. Operating systems and databases tend to have nice methods for initiating work at desired times. So having a stored procedure called when you need it is probably a capability already present in your environment. Hence all you need to do is put your desired code in the stored procedure. No need for you to hand craft the shceduling, initiation and management. One quite significant advantage of this approach is that you end up using a tehcnique that other folks in your environment already understand.

Writing the kind of code you're considering is very intersting and great fun, but in commercial environments is often a waste of effort.

djna
nice guitar +1 ;)
Oops
I was in doubt it was possible, but I'm trying to look also for different solutions.I didn't evaluate to use event programming, but this would be a bit more complex, because to do so I suppose I should:connect to the DB to establish a first session then wait for a QUIT event before terminate.connect to the DB once more in a different Thread and establish a second session that acts like the daemon process and generates the QUIT event.Once the first session terminates, its Thread could perform the communication with the remaining session.Do you suppose it could work?
Alessandro Rossi
@Oops - yes it's a Takamine LTD 99, only wish I could play it a little better!
djna
A: 

Make another jar for your other Main class and within your main application call the jar using the Runtime.getRuntime().exec() method which should run an external program (another JVM) running your other Main class.

Monis Iqbal
As I stated in the question, I can't use Runtime.exec() because I'm using a database stored procedure that is unaccessible from the OS.
Alessandro Rossi
A: 

The way you start subprocesses in Java is Runtime.exec() (or its more convenient wrapper, ProcessBuilder). If that doesn't work, you're SOL unless you can use native code to implement equivalent functionality (ask another question here to learn how to start subprocesses at the C++ level) but that would be at least as error-prone as using the standard methods.

I'd be startled if an application server like Oracle allowed you access to either the functionality of starting subprocesses or of loading native code; both can cause tremendous mischief so untrusted code is barred from them. Looking over your edit, your best approach is going to be to rethink how you tackle your real problem, e.g., by using NIO to manage the sockets in a more efficient fashion (and try to not create extra files on disk; you'll just have to put in extra elaborate code to clean them up…)

Donal Fellows