Title

Monday, 19 January 2015

Broadcast Message in Java


I need some mechanism that allows me to transfer some data from one java program to another within a same PC. I already investigated RMI but I want 1st app to broadcast some message for 2nd without request of 2nd application. In RMI only client can initiate a communication.

Raw sockets are also not desirable (very low level).

I need something like RMI with a different scheme of starting communication: 1 server broadcasts messages for several clients without requests from clients.

Could you please suggest me some libs/technologies (for desktop app)?

Answer

I suggest you to use java messaging service and one of it's implementations such as ApacheMQ.

A good starting point is here.

Answer2

I would suggest using a database with a trigger and store procedure. Here is an example of calling java methods from the database. A message queue will work, but that is an overly complex solution.

Here's an excerpt from an example of how to create a procedure and call it via a trigger:

First, you add the following Java method to the class DBTrigger    CREATE OR REPLACE PROCEDURE add_emp (   emp_no NUMBER, emp_name VARCHAR2, dept_name VARCHAR2)  AS LANGUAGE JAVA  NAME 'DBTrigger.addEmp(int, java.lang.String, java.lang.String)';    Then, you create the INSTEAD OF trigger:    CREATE OR REPLACE TRIGGER emps_trig  INSTEAD OF INSERT ON emps  FOR EACH ROW  CALL add_emp(:new.empno, :new.ename, :new.dname);
Answer3

ZeroMQ could be what you are searching, but I have only used it in C, C++ and Python, so I am not totally sure of how well it is usable in Java. But they have implemented the Publisher-Subscriber pattern for their sockets and at least the static library of the version 3.2.2 under ubuntu 12.04 is stable and works very well.

Answer4

As a "quick fix" write the output to file and have the second app read the file.

This is not elegant at all, but if you code to interfaces you can later replace the implementation with something better.

Answer5

A good solution would be an implementation of the OMG Data Distribution Standard (DDS). With DDS there is just a global data space with a dynamic discovery protocol. See for example RTI DDS, there is a free community edition.

No comments:

Post a Comment