2

Is it possible that a java program is running and it works on commands from another java program simultaneously running in same machine? For example: if a second java program sends a query database command to first running java program, the first one will execute a query in the database connected to it and reply back to second one.

Please help.

Thanks in advance.

arshajii
  • 127,459
  • 24
  • 238
  • 287
Raazan Kurunju
  • 89
  • 1
  • 1
  • 8

4 Answers4

1

You could use socket programming to do this. Make a server and make it listen to incoming messages.

http://docs.oracle.com/javase/tutorial/networking/sockets/

You could learn about sockets from the above link.

Sohaib
  • 4,556
  • 8
  • 40
  • 68
  • Does that make it difficult for sockets to function? I guess sockets are used to communicate between distinct processes? – Sohaib Jun 26 '13 at 16:29
  • Sorry my mistake, I intended comment to downvote another answer which has disappeared or has been edited meanwhile. I cannot cancel my downvote until your answer is edited. – C.Champagne Jun 26 '13 at 16:52
  • My answer is still down voted it seem.. Although I do believe sockets is not the ideal solution to this question. – Sohaib Jun 26 '13 at 17:00
  • Sorry, I got confused here...as i'm new in this site...isn't the sockets is best for my requirement? – Raazan Kurunju Jun 26 '13 at 17:39
  • Sockets is good enough for your requirement my mistake sorry what I meant to say was that you would have to make sure all your transactions are synchronized. If the usage of you application is not going to be much you can practically ignore that and just go ahead with a simple socket program. – Sohaib Jun 26 '13 at 17:42
  • Can you please tell me more about transactions synchronize..why is it necessary for my requirement? – Raazan Kurunju Jun 27 '13 at 09:33
  • Please vote up the answer. Synchronization is necessary in your case because if two clients interact with the database simultaneously you could end up in a spot. http://docs.oracle.com/cd/E12095_01/doc.10303/e12089/admsync.htm#CIHHCCEJ See that link for more information on synchronization. – Sohaib Jun 27 '13 at 11:39
0

It is possible.

Socket programming is good but in this case you have to implement synchronization and multi threading request handling.

Another way is using web service for storing data in db. http://docs.oracle.com/javaee/6/tutorial/doc/gijvh.html

Vitalii Pro
  • 313
  • 2
  • 17
0

As already answered you can use socket proramming but you would have to implement your own protocol.

It should be easier to use RMI which lets you invoke remote methods as if they were local but it is limited to java.

A "bit" heavier (in resources and implementation) solution is using web services but it is a standard which is not limited to java world.

You can also use JMS but I think it should be overkill (you need a server such as activeMQ)

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
0

Probably you can use any of remote invocations (RMI), see http://docs.oracle.com/javase/tutorial/rmi/ or manged beans (JMX), see http://docs.oracle.com/javase/tutorial/jmx/

Martin Strejc
  • 4,307
  • 2
  • 23
  • 38