Communication Between Two Separate Java Desktop Applications

Communication between two separate Java desktop applications

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;
}

});
}

}

Best way to communicate between java applications running on the same computer

You're options are basically

  • Through the file system

    A bit clunky, but easy to debug. Basically your main application would keep an eye on the file system (for instance by using a WatchService. More info here).

  • Through plain socktes

    Your main application opens up a socket and accepts connections from other applications that want to communicate with it. If all you want to do is to be able to shut down the main application, something as simple as

    new Thread() {
    public void run() {
    new ServerSocket(123456).accept();
    System.exit(0);
    }
    }.start();

    might do.

  • Over RMI

    Use an RMI registry to let your uninstaller do remote method invocations on your main application.

Assuming that you will build both the main application and the installer at the same time. And since they will run on the same JVM version and both on localhost I would definitely recommend RMI.

(Other OS-specific answers may include named pipes or signals, but I'd discourage you to go in this direction.)

Communication between jsp and java application

Database access through the Website is one of the essential components for any web-based development. JDBC, a mechanism that allows Java to talk to databases.

Java Database Connectivity (JDBC) is a standard Application Programming Interface (API) that is used to access databases, irrespective of the application driver and database product. In other words, JDBC presents a uniform interface to databases, however, if you change the database management system and your applications, you only need to change their driver. JDBC provides cross-DBMS connectivity to a wide range of SQL databases, and other tabular data sources, such as spreadsheets or flat files.

Here is the sample example:

import java.sql.*;
class DBQuery1{
public static void main(String args[]) throws SQLException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){};
Connection cnn;
Statement mystmt;
ResultSet myrs;
String op = "jdbc:odbc:JavaTest";
cnn = DriverManager.getConnection(op,
"Admin", "");
mystmt = cnn.createStatement();
String sql;
sql = "SELECT * FROM SupplierMaster " +
"WHERE SupplierCode IN ( " +
"SELECT SCode " +
"FROM Relation " +
"WHERE PCode IN ( " +
"SELECT ProductCode " +
"FROM ProductMaster " +
"WHERE ProdCatg IN ( " +
"SELECT CatgID " +
"FROM CategoryMaster " +
"WHERE CategoryName = 'Eatables')))";
myrs = mystmt.executeQuery(sql);
System.out.println(" "+"Sup Code" + " " +"Sup Name" + " " + "Sup Address ");
System.out.println("--------------------------------------------------------------------");
String name, add;
int code;
while (myrs.next())
{
code = myrs.getInt("SupplierCode");
name = myrs.getString("SupplierName");
add = myrs.getString("SupplierAddress");
System.out.println(" " + code +" " + name+" " + add);
}
}

}

More information you can find on this link:

http://webserver.ignou.ac.in/virtualcampus/adit/course/cst302/block2/cst302-bl2-u3.htm

Appropriate way of interfacing two Java applications

You can make use of REST webservices.

One will ask for the data, and other will provide data on request.

Sending an array is also possible in JSONArray in webservice.

Also webservices will be useful in desktop,web and mobile applications.

How two applications running on two different JVM will interact each other?

Reuben, you can use Web services or JMS

http://download.oracle.com/javaee/1.3/jms/tutorial/

Two-way communication between web application and desktop application

Is the 2.2 option even possible?

Yes

Which binding should we use for WCF services?

  1. For the incoming calls from the web app (href links) you should expose your service operation as a REST endpoint, using WCF webHttpBinding or something like Nancy, which is much lighter.

  2. For the polling, as already mentioned, you need to host another REST endpoint.

Two-way communication between web application and desktop application

Based on your description, this doesn't appear to be a genuine two-way requirement, as in duplex (calls going both ways). In both scenarios you outline the calls originate on the partner website. It's only the responses which travel the other way, or am I missing something?

The polling option is just an idea how to get state from desktop
application to web application

Strictly speaking, the polling originator is not the web app per se, but the client browser via JavaScript. Apart from any architectural concerns of using a client app as an intermediary between desktop and server, there is a very real complication around implementing cross-origin scripting in the browser.

I suggest a better solution would be to call the web app from the desktop app when state changes, and then have the web app "notify" the web client, either via ajax polling (to the web app), or something like SignalR.

This would likely be not much more work for your partner as although they would need to host a new "Status Changed" endpoint for you to call, the ajax polling task would probably be simpler seeing as they'd be polling their own service rather than yours.



Related Topics



Leave a reply



Submit