Home : J-Integra for .NET : HOWTO : How to Access .NET Event from Java
Q183233 - HOWTO: How to Access .NET Event from Java

How to Access .NET Event from Java

 

There are two ways to access .NET event from Java Clients:

  1. Registering the event listener (Java way to access event)
  2. Using .NET events (actual .NET events) from Java, wrapping .NET events using a delegate and an equivalent interface exposed to Java

We suggest using the first way to access .NET event, because it treats the .NET event as a normal Java event (add/remove event listener). The second way involves a wrapper class around the class using the events. It’s a little rough around the edges but it works.

Note: for the attached examples, please use Janetor tool to install your J-Integra for .NET license before your test. You can find the janet.xml for each test in \java_client directory after you unzip the attached examples.

1. Registering the event listener

Please find the attached java_csharp_event.zip for this example.

A. Write the .NET Server with event handler:
using System;
using System.Runtime.InteropServices;
using System.Collections;
namespace csharp_server
{
 public class Factory : MarshalByRefObject
 {
  private ArrayList listeners;
  public Factory()
  {
   Console.WriteLine("Factory Created...");
   listeners = new ArrayList();
  }
  public void addMyEventListener(ITestListener l)
  {
   listeners.Add(l);
  }
  public void removeMyEventListener(ITestListener l)
  {
   listeners.Remove(l);
  }
  public void notifyListeners(TestMessage message)
  {
   if (!message.Message.Equals(""))
   {
    Console.WriteLine(
     "Sending \"" + message.Message + "\" from " + "\"" + message.Name + "\""
     + " to " + listeners.Count + " clients.");
    IEnumerator iterator = listeners.GetEnumerator();
    iterator.Reset();
    while (iterator.MoveNext())
    {
     ITestListener l = (ITestListener)iterator.Current;
     l.onMessage(message);   // Calls the Java implementation of onMessage() method
    }
   }
  }
 }
}

Note: TestMessage object is marked as Serializable (TestMessage class can be find in the attached example package). This allowed .NET and Java to pass this object by value across the wire.

B. Write the Java Client to register .NET event:

...
TestListener testListner = new TestListener();
f.addMyEventListener(testListner);
TestMessage message = new TestMessage();
message.setmessage("Hello");
message.setname("Java Client");
f.notifyListeners(message);
f.removeMyEventListener(testListner);
...
 

2. Wrapping .NET events using a delegate

Please find the attached java_csharp_event_wrapper.zip for this example.

A. Write the .NET Server with a delegate:

...
public delegate void myeventhandler(string str);
public class ServerClass: MarshalByRefObject
{
   public ServerClass ()
   {
    System.Console.WriteLine("Created Server");
   }
   public string myfunc(string what)
      {
            FireNewBroadcastedMessageEvent("Event: " + what + " was said");
            return "Take " + what;
      }
      public event myeventhandler myHandler;
      protected void FireNewBroadcastedMessageEvent(string text)
      {
            Console.WriteLine("Broadcasting Message \"" + text + "\"");
            if (myHandler != null)
            {
                myHandler("Hello!");
            }
            Console.WriteLine("Done Broadcasting...");
      }
}
...
 

B. Write the .NET Server Wrapper with .NET event handlers:

using System;
using System.Collections;
namespace csharp_server
{
    public class ServerWrapper : MarshalByRefObject
    {
        private Hashtable eventList = new Hashtable();
   
        public ServerWrapper()
        {
    System.Console.WriteLine("Created ServerWrapper");
   }
        public void add_myHandler ( IMyDelegate imd, ServerClass scs )
        {
    if ( eventList.Count == 0)
            {
                scs.myHandler += new csharp_server.myeventhandler(myeventhandler);
            }
            if (!eventList.Contains(imd))
            {
                eventList.Add(imd, scs);
            }
        }
   
        public void remove_myHandler ( IMyDelegate imd, ServerClass scs )
        {
            if (eventList.Contains(imd))
            {
                eventList.Remove(imd); 
            }
            if ( eventList.Count == 0) 
            {
                scs.myHandler -= new csharp_server.myeventhandler(myeventhandler);
            }
        }
   
        public void myeventhandler ( string str )
        {
            ServerClass sc = null;
            IDictionaryEnumerator en = eventList.GetEnumerator();
            while (en.MoveNext())
            {
                sc = en.Value as ServerClass;
                if (sc != null)
                {
                    try
                    {
                        ((IMyDelegate)en.Key).myeventhandler(str);
                    }
                    catch (Exception e)
                    {
       System.Console.WriteLine(e.Message + "\n" + e.StackTrace);  
                    }
                }
            }
        }
    }
}
 

C. Write the .NET delegate Java implementation:

public class MyDelegate implements IMyDelegate {
  public void myeventhandler(String str) {
    System.out.println("Callback: " + str);
  }
}
 

D. Write the Java Client add/remove .NET event using delegate:

 ...
 ServerClass sc = new ServerClass();
 MyDelegate md = new MyDelegate();
 ServerWrapper sw = new ServerWrapper();
 sw.add_myHandler(md, sc);
 sc.myfunc("Java Client");
 sw.remove_myHandler(md, sc);
 ...
 

Related Articles
No Related Articles Available.

Article Attachments
java_csharp_event.zip
java_csharp_event_wrapper.zip

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 1/8/2007.
Last Modified on 1/16/2007.
Last Modified by J-Integra KB Admin.
Skill Level: Intermediate.
Article has been viewed 7744 times.
Rated 8 out of 10 based on 3 votes.
Print Article
Email Article