Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Design for intreraction between two unrelated modules.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application there are two unrelated modules.

Now there is a scenario in which I want to send some notification from one module to another if some particular condition is met.

Using messaging service it is easy but we are not using it in our application.

How this can be implemted in java is there any particular API through which it can be achieved?

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends. Do they run on the same machine? The same JVM? One way which I like is to open a socket connection between the two in the case they run on different JVMs.
 
Ranch Hand
Posts: 81
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use socket as suggested by John.

You can also use plain java RMI. quite easy and simple method.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is on the same JVM.

Suppose there are two modules A and B and I want to send notification to B from A.

one way is to get the reference of B in A and invoke B's method but that is not the right
approach because i want to keep it generic and do not want to add any dependecies and more over
module B acts as pluggable component which may or may not be there.

Is there other approach for this or any design pattern which can be helpful?



 
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Publisher/subscriber.

Let A have a register method that accepts some sort of subscriber interface S (also known as a listener). A will keep track of a list of these subscribers.
When a change happens, A will then call the appropriate method in S of each subscriber.

B simply implements S, or has a helper class (tip: anonymous local class) that implements S and registers itself (or it's helper class) through A's register method.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see the Observer Pattern.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic