Mutual invocation of Java and. Net--using JNBridge bridging mode for remote communication

Source: Internet
Author: User
Tags soap visual studio 2010

History of distributed development

It is a common way of network development to use remote object to make communication between server and client, and in. NET and Java development, long-distance remote objects are already well supported (please refer to friends who are interested in remotely remote object invocation Using remote objects for distributed development ).

Starting from 2003. NET is rife with the distributed development of. NET Remoting remote object invocation,. NET remoting is primarily used to manage synchronous and asynchronous RPC sessions across application domains. By default, Remoting can use the HTTP or TCP protocol for information Communication and use XML-encoded SOAP or binary message formats for data exchange.. NET Remoting provides a very flexible and extensible programming framework and can manage the state of objects. After the introduction of the Framewok2.0, WCF flourished, it is a combination of. NET Remoting remote objects, TCP/IP sockets, Web services, msmq,p2p Point-to-point and other types of communication products, WCF enables remote communications into another step.

In Java, there is also strong support for remote communication, which defines the RPC (remote Procedure call Protocol) protocol, a remote procedure invocation protocol that can be computed from a service requested by a remote computer over a network. It can use communication methods such as TCP or UDP to pass information data between programs without needing to know the protocol of the underlying network technology. In the network communication model, RPC spans the transport layer and the application layer. RPC makes it easier for a system to implement distributed development. However, RPC communication does not implement object-oriented development principles, and RMI (remote method invocation) is a mechanism for communication between computers using remote objects to invoke each other. It uses the RPC protocol to make remote calls to the server and client objects in an object-oriented way.

However, there is a disadvantage in the. NET Remoting and RMI communication, which is the limitation of the development language, regardless of which communication method is used, both the server and the client must support the same development language. Communication cannot span the limits of the development language, which is a frustrating message for a person. Because in large-scale development projects, the development of different modules is often assembled in different languages. Using remote for distributed development can improve communication efficiency but is limited. In this respect, the major development companies made a positive contribution to the development of such as J-integra (aka Ja.net), Iiop.net (Internet inter-orb Protocol), JNBridge and other integrated development tools, to. NET and Java to enable the mutual invocation of remote objects to provide sufficient support. Below is a detailed introduction to "JNBridge implementation of. NET and Java interoperability".

JNBridge Overview

JNBridge is a leading Java and. NET interoperable product, and with JNBridge technology, Java and. NET code can implement object sharing without the need for cross-compiler. All Java code runs on the JVM, while. NET code runs on the CLR. In this scenario, the JVM and the CLR can run on different machines, run on different processes in a single machine, and even run on different application domains of the same process. After years of development, JNBridge has released JNBridgePro 5.0,jnbridgepro 5.0 with more powerful features.

    1. Supports Ava and. NET cross-platform transactions;
    2. Support for Microsoft Visual Studio and Eclipse plug-ins;
    3. Compatible with Windows 7;
    4. Cross-platform transaction integration is mainly transparent to users;
    5. ' Rollback '-termination of either party will result in the rollback of the actions of both parties;

JNBridge supports. NET to Java, Java to. NET Two kinds of service, and can line with TCP, HTTP, soap and other protocols for both parties to communicate, the following is a "Java call. NET" to implement a simple development example, introduce the function of JNBridge.

JNBridge Configuration

First you can download the program on JNBridge's official website http://www.jnbridge.com/downloads.htm, install JNBridgePro 5.0, start Jnbproxy v5.1.exe, select Create New Java->.net project, after creating a new project, click Project->java Options to configure the system. First set the path of the native Java.exe application and the Jvm.dll assembly, and then set the path to Jnbcore.jar and Bcel.jar (in 5.1, these two files exist in the/jnbridge/jnbridgepro V5.1/ Jnbcore/"), finally can choose HTTP or TCP communication mode (in this case, select TCP Communication, the system default interface is 8085 you can also choose a custom interface). Click "OK" button, when the configuration is complete, the system will automatically generate a "/jnbridge/jnbridgepro v5.1/jnbcore/jnbcore_tcp.properties" file to record TCP configuration information.

. NET Server-side development

If you are using Visual Studio 2008 or Visual Studio 2010, the system will be aware of the presence of JNBridge, and you can create a new Dotnetjavaproxies project directly when you create a new project. Here you want to show the conversion performance of JNBridge, so it's a straightforward new solution. Add a model project, join the person class, add the serializable attribute to the person, and note that you want to remove unnecessary references, because references to the framework change to the corresponding package at the time of conversion.

Using System;

Namespace Model
{
[Serializable]
Publicclass person
{
Publicint ID
{
Get
Set
}

Publicstring Name
{
Get
Set
}

Publicint Age
{
Set
Get
}
}
}

Add a manager project, join the Personmanager class, and, when testing, just put the dummy data in the Datasource.sour file.

Using System;
Using Model;
Using System.IO;
Using System.Runtime.Serialization.Formatters.Binary;
Using System.Collections.Generic;

Namespace Manager
{
Publicclass Personmanager
{
Virtual data sources
Private list<person> DataSource ()
{
FileStream FileStream =new FileStream ("./datasource.sour", FileMode.Open, FileAccess.ReadWrite);
BinaryFormatter Formatter =new BinaryFormatter ();
List<person> personlist= (list<person>) formatter. Deserialize ((FileStream));

return personlist;
}

Get all the person
Public list<person> GetList ()
{
list<person> personlist = DataSource ();
return personlist;
}

Get the corresponding person based on the input ID
Public person Getpersonbyid (int id)
{
foreach (Person person in DataSource ())
{
if (person.id = = ID)
return person;
}
Returnnull;
}
}
}

Add a consoleapplication as the startup project, add a reference to the JNBShare.dll in the project (file path "\jnbridge\jnbridgepro v5.1\4.0-targeted"), and then add the configuration file

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<sectiongroup name= "JNBridge" >
<section name= "Dotnettojavaconfig"
Type= "System.Configuration.SingleTagSectionHandler"/>
<section name= "Javatodotnetconfig"
Type= "System.Configuration.SingleTagSectionHandler"/>
<section name= "Tcpnodelay"
Type= "System.Configuration.SingleTagSectionHandler"/>
<section name= "Javasidedeclarations"
Type= "System.Configuration.NameValueSectionHandler"/>
<section name= "Assemblylist"
Type= "Com.jnbridge.jnbcore.AssemblyListHandler, Jnbshare"/>
</sectionGroup>
</configSections>
<jnbridge>
<dotnettojavaconfig scheme= "jtcp" host= "localhost" port= "8085"/>
<!--Note that the communication interface must be consistent with the interface configured in the JNBridge-
<javatodotnetconfig scheme= "jtcp" port= "8086"/>

<!--register a common assembly--
<assemblyList>
<assembly file= ". \model.dll"/>
<assembly file= ". \manager.dll"/>
</assemblyList>
</jnbridge>
</configuration>

Finally start the service

Using System;
Using Com.jnbridge.jnbcore;

Namespace Net_service
{
Class Program
{
Staticvoid Main (string[] args)
{
Console.WriteLine (". NET start!");
Start. NET-side service, note that you must add a reference to Jnbshare before you can use Com.jnbridge.jnbcore.DotNetSide
Dotnetside.startdotnetside ();
Console.readkey ();
Shut down. NET-side Services
Dotnetside.stopdotnetside ();
}
}
}



Generate the Transformation Layer code

Open JNBridge, select the toolbar "Add class from assembly Files" and add Model.dll,manager.dll and mscorlib.dll (this assembly exists in "C:\Windows\ Microsoft.net\framework\v4.0.30319\ ", which contains important namespaces such as System,system.collections,system.io, etc.)

After selecting the necessary classes, press "Project->build" and the system will select the appropriate. NET generates a Proxy.jar agent package for class.

Java-side development

Create a new Java project that references the newly generated agent package, Proxy.jar, and Jnbcore.jar, Bcel-5.1-jnbridge.jar (in version 5.1, these two files exist in the/jnbridge/jnbridgepro V5.1/ Jnbcore/"), copy the JNBridge TCP configuration file jnbcore_tcp.properties into the Bin folder and develop a Test side. It is important to note that when generating the Proxy.jar code package, it is necessary to add references to the space of these common classes such as system.collections.generic,system.string and so on, otherwise it is not possible to invoke the system.collectio in. NET inside Java. Ns. Generic.list such as these common classes. Since in Java there is a conflict between class names for the common classes like list, So in the proxy will System.Collections.Generic.List these classes named System.Collections.Generic.List_1.

Package com.jnbridge.javaclient;

Import com.jnbridge.jnbcore.*;

Import system.collections.generic.*;
Refer to proxy class inside system.collection.generice.* space inside Proxy.jar Package

Import manager.*;
Import model.*;

Publicclass Test {

/**
* @param args
*/
Publicstaticvoid Main (string[] args) {
TODO auto-generated Method Stub
try{
Registering the JNBridge TCP profile Jnbcore_tcp.properties
Dotnetside.init ("E:\\java projects\\jnbridge java\\java client\\bin\\jnbcore_tcp.properties");

Personmanager personmanager=new Personmanager ();
Note that this list_1 is the proxy class for System.Collections.Generic.List within the Proxy.jar package, not the list class that comes with the JDK
List__1 personlist=personmanager.getlist ();
if (Personlist.get_count ()!=0) {
Note this get_count () method is the GetCount () method of the System.Collections.Generic.List proxy class within the Proxy.jar package
for (int n=0;n<personlist.get_count (); n++) {
Person person= (person) personlist.get_item (n);
System.out.println ("Id:" +person. GET_ID () + "Age:" +person. Get_age () + "Name:" +person. Get_name ());
}
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}

When the Java end is complete, start it first. NET Server, and then start the Java side, Java will call to the. NET end of the data, the screen will show the test results:

id:0 Age:29 Name:leslie
Id:1 age:23 Name:rose
Id:2 age:22 Name:jack

There are many examples of development on the JNBridge website, which are not introduced here. The conversion of different development types requires the use of different toolkits, and you can refer directly to the textbooks on the official website.

Java and. NET Hybrid development model

In many large-scale system development, development tools are often not limited to the same development language, but will use a variety of development language mixed-type development. As in the process of developing the underlying and business layers, due to the. NET is limited to working on Windows systems, and Java can be used flexibly in Windows, Unix, and Linux to be more popular. However, in developing the UI presentation layer, it is obvious that development tools such as swing and applets are constrained more, and development tools such as WPF, Siverlight, and WinForms are popular because of their mature controls and gorgeous pages. So use Java to develop the UI layer of the underlying, business layer, and Linux systems, while using. NET to develop the UI layer of Windows systems is a common example. In this development process, the use of JNBridge tools to generate proxy proxies can greatly reduce the difficulty of development, improve development efficiency, so that the. NET platform and the Java platform for seamless connectivity.

Sum up JNBridge can be seen as a bridge between the Java platform and the. NET platform, and the above example leverages the seamless connection between the JNBridge reality Java and. NET, so that the. NET client does not need to perceive the Java underlying existence, and the. NET UI side directly calls the proxy proxy to communicate with the Java side , which is a good way to call each other between Java and. Net.

Source code (download)

Related articles

Java and. NET Mutual invocation--TCP/IP sockets to call each other's basic schema (with the original code)

Mutual invocation of Java and. NET-calls through Web services (with original code)

Mutual invocation of Java and. Net--using JNBridge bridging mode for remote communication

Mutual invocation of Java and. Net--using JNBridge bridging mode for remote communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.