Access Java Events from .NET Using J-Integra® for .NET |
This example demonstrates how to access Java Events from .NET client. J-Integra® for .NET is a Java interoperability component that bridges Java/J2EE and Microsoft .NET. It provides bi-directional access of Java objects and .NET components.
Use this example as a guideline to show you how to access Java events using J-Integra for .NET.
This example illustrates a very simple multi-client chat program. Each .NET client will access a Java Singleton and add itself as a listener. An event occurs when a message is typed by one of the .NET clients. Each message is then sent to every listening .NET client.The Steps to Follow will show you how to deploy J-Integra® for .NET correctly for this example. Refer to the diagram below for an overview of the scenario:
A platform with Microsoft Visual Studio .NET 2003 (7.1.3088 or higher) installed, or at least the .NET Framework 1.0.3705 or higher. This will be known throughout the example as the .NET platform.
A platform with J-Integra for .NET installed. This will be known throughout the example as the Java platform.
A version of JDK (1.3.1 or higher) installed on the Java platform, and the bin directory included in the path. This required to compile and run the Java server.
Verify that GenService is installed correctly on the .NET platform.
Perform this step on the Java platform.
In this step, you will use the GenNet tool to generate .NET proxies for the Java classes.
The first step is to compile the Java source. At the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:
javac -classpath . java_server\*.java
Launch GenNet from the Start Menu.
Change directory to $USER_INSTALL_DIR$ and then enter:
java -jar lib\gennet.jar
The first screen of GenNet shows a list of machines that have GenService running (screen shot not shown). Select the appropriate one and click Next.
The next screen prompts for the components (JAR files or directories)
to be added to the CLASSPATH. Each of these components are required
in order for the Java runtime to resolve references to Java classes when
generating .NET proxies. You'll need to add the following component:
- $USER_INSTALL_DIR$\examples\csharp_java_events
Move on to the next step by clicking Next.
The next screen prompts for the specific Java classes that will
have .NET proxies generated for them. Click on Browse...
to add the following classes (use the CTRL key to select
multiple classes):
- java_server.Message
- java_server.Messenger
- java_server.MyEventListener
Make java_server.Message a "marshal-by-value" object by clicking on Options... and checking Pass by value.
Move on to the next step by clicking Next.
The next screen prompts you to specify the .NET Assembly name along with the output directory. The screen shot may show a different output directory than the value of Output Directory shown here, but it should be pointing to $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug.
You should see a message that the proxy generation was successful. Click Exit to exit GenNet.
Perform this step on the .NET platform.
In this step, you will need to compile the .NET client code.
Load Client.sln located in $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client with Visual Studio .NET 2003. Press Ctrl + Shift + B to build. If successful, this should build Client.exe in the bin\Debug directory.
Perform this step on the Java platform.
In this step, you will use the GenJava tool to generate Java proxies from the .NET Assembly. These proxies are used when a message event is fired and J-Integra for .NET calls the event handler of each subscribed .NET client.
Launch GenJava from the Start Menu.
Change directory to $USER_INSTALL_DIR$ and then enter:
java -jar lib\genjava.jar com.intrinsyc.janet.genjava.GenJavaApp
The first screen of GenJava
prompts for the location of the .NET assembly along with the output directory
where the Java proxies will go. They should be (respectively):
- $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Client.exe
- $USER_INSTALL_DIR$\examples\csharp_java_events
Move on to the next step by clicking Next.
You should see a message that the proxy generation was successful. Click Finish to exit GenJava.
To compile the Java proxies, at the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:
javac -classpath $USER_INSTALL_DIR$\lib\janet.jar;. csharp_client\*.java
Perform this step on the Java platform.
In this step, you will need to configure J-Integra for .NET as a server so it can respond to incoming TCP requests. This is done through the Janetor tool.
Launch Janetor from the Windows Start Menu.
java -jar $USER_INSTALL_DIR$\lib\janetor.jar
Go to File - Open and open janet.xml located in $USER_INSTALL_DIR$\csharp_java_events.
Install your J-Integra for .NET product license. See Installing the J-Integra® for .NET license for further information.
Select java_server.Messenger under Local Objects. Here you see that J-Integra for .NET exposes this class as a Singleton at the given machine address and port using TCP. The default node above exposes the remaining classes with the same configuration details, except that they are not Singletons.
Select default under Remote Objects. Here you see that J-Integra for .NET is configured as a client to access a CAO using TCP binary. Notice that URI is blank - this is because there is no one specific endpoint for J-Integra for .NET to call the .NET client's event handler. This is fine, since the client will pass over the correct URI when it first accesses the J-Integra® for .NET server.
Before you exit Janetor, save the configuration by going to File - Save.
Perform this step on the Java platform.
In this step, start the J-Integra for .NET server.
At the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:
java -cp $USER_INSTALL_DIR$\lib\janet.jar;. com.intrinsyc.janet.Main
Perform this step on the .NET platform.
In this final step, you will configure the .NET client(s) using a configuration file, then compile and run it.
Modify remoting_tcp.config in $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug to look like:
<configuration> <system.runtime.remoting> <application> <client> <wellknown type="java_server.Messenger, JanetExample" url="tcp://localhost:8765/java_server.Messenger.soap"/> </client> <channels> <channel port="0" ref="tcp"/> </channels> </application> </system.runtime.remoting> </configuration> |
Notice that tcp://localhost:8765 matches what was entered under Local Objects in Janetor. Change accordingly if necessary.
We will now launch two different chat clients. In general, the syntax is:
Client [username]
Launch a command prompt, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug and enter:
Client Tom
Launch another command prompt, and in the same directory as above, enter:
Client Jen
At this point, you should see two different client processes started. Type a message in one of the clients and you will see that all clients receive it:
After a client sends a message, take a look at the J-Integra for .NET server console. You should see the message received and sent back out:
Sending "Hi Tom" from Jen to 2 clients. Sending "Hi Jen, how are you?" from Tom to 2 clients.
© 2007 Intrinsyc Software International, Inc. All rights reserved. Legal |