Skip to main content

Communication between two separate Java desktop applications


I'm looking to develop two separate (but related) Java desktop applications.



I want the ability for one application to trigger the other, passing in data that can then be edited and passed back, i.e. the communication will be two way. If the other application is already running I want them to just communicate, i.e. I dont want to just pass arguments over the command line, etc.



Generally speaking, what strategies/techniques should I be looking at in order to achieve this?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. To show how easy it is to let two applications communicate with each other, check out this network-clipboard demo using JGroups. Just start two instances and begin dropping files into one of them. The second instance will instantly show the same files.

    import java.io.Serializable;
    import java.awt.*;
    import java.awt.datatransfer.*;
    import javax.swing.*;
    import org.jgroups.*;

    public class JGroupsTest {

    public static void main(String[] args) throws Exception {
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(500, 300);
    final DefaultListModel listModel = new DefaultListModel();
    final JList panel = new JList(listModel);
    panel.setBackground(new Color(128, 0, 40));
    panel.setForeground(new Color(240, 240, 240));
    frame.add(panel);
    System.setProperty("java.net.preferIPv4Stack", "true");
    final JChannel channel = new JChannel("udp.xml");
    channel.connect("networkclipboard");
    channel.setReceiver(new ReceiverAdapter() {
    @Override
    public void viewAccepted(View newView) {
    frame.setTitle("Network Clipboard - " + channel.getLocalAddress());
    }

    @Override
    public void receive(Message msg) {
    listModel.addElement(msg.getObject());
    }
    });

    panel.setTransferHandler(new TransferHandler() {
    @Override
    public boolean importData(JComponent comp, Transferable t) {
    DataFlavor[] transferDataFlavors = t.getTransferDataFlavors();
    for (DataFlavor flavor : transferDataFlavors) {
    try {
    Object data = t.getTransferData(flavor);
    if (data instanceof Serializable) {
    Serializable serializable = (Serializable) data;
    Message msg = new Message();
    msg.setObject(serializable);
    channel.send(msg);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return super.importData(comp, t);
    }

    @Override
    public boolean canImport(TransferSupport support) {
    return true;
    }

    @Override
    public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
    return true;
    }

    });
    }

    }

    ReplyDelete
  2. They could each listen on a Socket. This tutorial is good to get started.

    ReplyDelete
  3. You should also consider good ol' classic RMI.

    ReplyDelete
  4. Have a look at JavaGroups, it will solve your communication problem and also help you to detect if the other app is running. If the app isn't running you will have to launch a new JVM for it with java.lang.Runtime.exec()...

    ReplyDelete
  5. It depends how would you like to communicate those 2 programs:


    If you need only inter-process semaphores, create a file somewhere in /tmp and lock it.
    If you need only inter-process synchronous messaging (remote procedure call), RMI should be easiest.
    If you need asynchronous interprocess messaging, JMS should be easiest.
    If you need inter-process shared memory, use mapped files.
    If you need all the above, Terracotta (http://www.terracotta.org/ ) is the easiest way: Java programs on different JVMs on the same or even different computers see each other as if they were executed inside one JVM on one machine. Splitting one program into a few doesn't even require any code changes - it's enough to write an XML config file.

    ReplyDelete
  6. To keep things simple why not just use plain TCP sockets?

    ReplyDelete
  7. The "Enterprise" way to go would be to run these apps in a JEE server or at least in a Spring framework. It's also probably vastly overkill.
    If a bunch of data needs to be communicated, then RMI will do it.
    If you're not afraid to hack your own protocol, data structure and error handling, you can set up server and client sockets and communicate through those.
    I think there's a certain crude appeal to the alternative of communicating via a file in a common directory (setting up your own protocol of who writes or erases the file when), or via a shared database. Low-tech, not extremely fast, but very simple and reliable. And it's fairly easy to monitor "communication" from the outside.

    ReplyDelete
  8. Try to communicate with SocketCommunication, even if the application are in the same machine.

    Here can find more info about how to do it (Sun/Java documentation).

    ReplyDelete
  9. I second Socket communication and RMI. RMI is a little more involved but is more intuitive to a programmer. It depends on what type of information you are sending though. Pushing raw bytes over to another machine might make more sense then running the RMI server and dealing with all that jazz...

    ReplyDelete
  10. Depending on what style of communication you're looking for (high latency, lots of data, etc.) and whether or not this system may expand past simply 2 java systems, a possibility could be a messaging system using a middleware solution such as Tibco SmartSockets.

    Any more info on your setup and expectations would help.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex