I have two processes written in java in two machines within my network that should pass simple chunks of data to each other.
I'm looking for a quick and dirty way (without resorting to writing files and polling for changes on network share files)
java RMI (remote method interface)
Assuming the machines have each other's addresses (getting said addresses is a different issue), I think your best bet is Spring remoting. You have a data passing interface, a domain object that encapsulates the data you want to pass, and a remoting service implementing that interface on each side. After that, your code treats the remote service as just another collaborator. Cheap, easy, fast, and you can use whatever transport you like (RMI, HTTP, Hessian, Burlap etc).
It usually takes me about 10 minutes (at most) to expose an existing interface/service as a remoting service, though first time will take a little longer.
JMS over the ActiveMQ Broker is very easy. Its available here. The following is quick and dirty pseudo code to send a message, but as you can see its minimal code. Consider two Java apps, a publisher and a subscriber.
Create a broker in one of your publisher:
BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.setPersistent(false);
broker.start();
Setup connections in both applications (Note: the subscriber will replace local host with the address of the machine running the broker):
final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
TopicConnection connection = factory.createTopicConnection();
Session session = connection.createSession(false,Session.DUPS_OK_ACKNOWLEDGE);
Send data in the publishing application:
Topic topic = session.createTopic("Quick and dirty topic");
MessageProducer producer = session.createProducer(topic);
ObjectMessage message = session.createObjectMessage();
message.setObject((Serializable) "Object you want to send");
producer.send(message);
In you subscribing app (which must implement javax.jms.MessageListener) the following setup is needed:
Destination dest = session.createTopic("Quick and dirty topic");
MessageConsumer consumer = session.createConsumer(dest);
consumer.setMessageListener(this);
The subscribing app's onMessage(Message arg0)
method (which it has because its a MessageListener) will be notified of the published objects.
What you have described is the quick (and not really that quick) and dirty way. The only other ways of accomplishing the task are setting up the processes to listen and send data through sockets (be that implemented directly by you - in java it is very easy, just Google and you will find, or as part of an library - probably needed for anything more complex)
So each process would have a listener on a specific port and every so often would send data across that port.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With