Croydon Dias
Croydon Dias

Reputation: 1936

Trigger functions from another java program

We've got a server app and two stand alone client apps (both with different functionality - one for front office and the other for back office). Everything is written in Java.

What we need right now: If both apps are running - click on a button in one app -> checks to see if the other app is open and triggers some functionality (display a message, open a frame) on that app

if the other app isn't open -> it should display a message saying so.

Can anyone point me in the right direction to achieve this. The best real life example I can give is: how clicking on the an itunes link in the web browser opens the iTunes application if installed and to the relevant appstore page.

EDIT: Our applications don't deal with websites at all. Everything uses Swing.

Upvotes: 2

Views: 615

Answers (1)

Bruno Grieder
Bruno Grieder

Reputation: 29854

There is no "best" way to achieve inter-app communications but there are many ways; the best one will be the one that fits best your environment: network conditions, firewalls, number of calls, synchronous vs asynchronous, etc...

Usually communication is achieved using either:

  • Remote Procedure Calls: an app basically calls a function/method on the other app and passes arguments. RPC are usually synchronous: the response is sent within the same communication/transaction
  • Messaging: an app sends messages to the other app which, maybe, replies with other messages. Messaging is usually asynchronous. The frontier between the two can be pretty blur with some protocols like REST.

In the Java world,

  • RPC is usually achieved using either

    • RMI: Java only solution; easy to implement; does not like firwalls much.
    • SOAP Web services: not Java centric; hard to implement; full of traps; network friendly.
  • Messaging can be achieved using

    • JMS: Java only; rather easy to implement but asynchronous; extremely powerful on high loads
    • JSON/XML HTTP/s Messaging: there are many protocols here from the most secure like AS2 to RNIF, plain XML/Json POST etc... These are network and language agnostic but always require some work to implement.

An hybrid approach is REST which has become very popular due to the benefits of an easy implementation and network friendliness but has the drawbacks of not being very formalized. it is a technology rather than a specification. I would look at documentation around JAX-RS and frameworks like Restlet and Jersey to get you started.

(Edit) I purposely did not mention developing your own with Java sockets. IO is by definition impure and often multithreaded: IO is very hard to get right. If you really insist going down that route, at least, use the help of a proper framework like Apache Mina or Netty.

Upvotes: 5

Related Questions