Problem with JNI and Tomcat

Discussion in 'Java' started by Elvandar, Apr 10, 2007.

  1. Elvandar

    Elvandar Guest

    Hi all.
    I developed a JNI DLL to use with a Java Web Application.
    This DLL is used to communicate over a RS232 port, and it must notice
    any event that occurs on the port to the java application.
    I tested it before with a Standalone app to make sure all works, and
    then moved it to the web application. The problem is that in the
    standalone app it's all ok, whilst in the web app I'm only able to call
    JNI functions from java, but I can't call java methods from the JNI
    DLL...here is the code involved, if you have any idea on how to solve
    this problem is very appreciated!!!

    The sequence of operations is the following:

    1) From it.ribes.serialDriver.SerialPort class i call the
    nativeOpenPort JNI function
    2) the nativeOpenPort functions looks for the "notifyEvent" Java method
    in the object that called it, in order to notify events when they
    occurs, and saves it in the "eventNotifyMethod" of the SERIALPORT
    structure
    3) When an event occurs, the thread used for reception calls the
    "notifyEvent" function in the DLL, which calls the Java method searched
    in the step 2

    The CSerialEx Class istansiate a thread which "listens" to the serial
    port waiting for events, and when an event occurs calls the
    "notifyEvent" in the DLL.

    Thanks for any help, I'm a bit desperate... :(


    ==========
    JAVA
    ==========

    --- SerialPort.java ---

    public class SerialPort extends CommPort {
    SerialPortEventNotifier notifier;

    ...

    private native boolean nativeOpenPort(String port);

    /* Called from JNI DLL */
    private void notifyEvent(int eventType) {
    notifier.notifyEvent(eventType);
    }
    /* *********** */

    boolean open(int timeout) {
    return nativeOpenPort(this.port);
    }

    ...

    }


    --- CommPortIdentifier.java ---

    public class CommPortIdentifier {
    static List portList = new LinkedList();

    public static final int PORT_SERIAL = 1;

    private String portName;
    private int portType;

    static {
    System.loadLibrary("RS232Driver_DLL");
    }

    private static native boolean nativeTestSerial(int port);

    public CommPort open(String application, int timeout) throws
    PortInUseException {
    SerialPort serialPort = new SerialPort(this.portName);
    if(!serialPort.open(timeout))
    throw new PortInUseException(portName);
    return serialPort;
    }

    ...

    }

    =============
    JNI DLL
    =============

    --- it_ribes_serialDriver_serialPort.cpp ---

    ....

    typedef struct
    {
    CSerialEx *com;
    char port[10];
    jobject javaEventManager;
    jmethodID eventNotifyMethod;
    LPSTR rxBuffer;
    int rxBufferDim;
    int inputIndex;
    int outputIndex;
    int dataAvailable;
    HANDLE readingMutex;
    int rxTimeout;
    DWORD eventMask;
    } SERIALPORT, *LPSERIALPORT;


    /* This method is used to open the serial port */
    JNIEXPORT jboolean JNICALL
    Java_it_ribes_serialDriver_SerialPort_nativeOpenPort (JNIEnv *env,
    jobject jobj, jstring port)
    {
    LPSERIALPORT SerialPort = (LPSERIALPORT)malloc(sizeof(SERIALPORT));

    if(VM == NULL)
    env->GetJavaVM(&VM);

    LPSTR comPort = (LPSTR)env->GetStringUTFChars(port, 0);

    _strupr(comPort);
    strncpy(SerialPort->port,comPort,10);
    SerialPort->com = new CSerialEx(); // This is the class used to
    communicate over the serial port (Overlapped I/O used)

    SerialPort->com->Open(comPort,0,0,true);
    SerialPort->javaEventManager = env->NewGlobalRef(jobj);
    jclass cls = env->GetObjectClass(SerialPort->javaEventManager);
    SerialPort->eventNotifyMethod =
    env->GetMethodID(cls,"notifyEvent","(I)V");

    env->ReleaseStringUTFChars(port, comPort);

    return TRUE;
    }

    /* This method is called from the CSerialEx class to notify an event */
    void NotifyEvent(CSerial::EEvent event, LPSTR port)
    {
    void *voidenv = NULL;
    JNIEnv *env;

    LPSERIALPORT SerialPort = SearchSerialPort(port);
    int javaEvent = MapEvent(event);

    VM->AttachCurrentThread(&voidenv,NULL);
    env = (JNIEnv *)voidenv;

    if(javaEvent != 0)
    {
    env->CallVoidMethod(SerialPort->javaEventManager,
    SerialPort->eventNotifyMethod, javaEvent);
    }

    VM->DetachCurrentThread();
    }

    ....
     
    Elvandar, Apr 10, 2007
    #1
    1. Advertising

  2. Elvandar

    Elvandar Guest

    Elvandar pretended :
    > Hi all.
    > I developed a JNI DLL to use with a Java Web Application.
    > This DLL is used to communicate over a RS232 port, and it must notice any
    > event that occurs on the port to the java application.
    > I tested it before with a Standalone app to make sure all works, and then
    > moved it to the web application. The problem is that in the standalone app
    > it's all ok, whilst in the web app I'm only able to call JNI functions from
    > java, but I can't call java methods from the JNI DLL...here is the code
    > involved, if you have any idea on how to solve this problem is very
    > appreciated!!!
    >
    > [CUT]


    If it could be useful, I use Java 1.4.2 and Tomcat 5.0.28.
     
    Elvandar, Apr 10, 2007
    #2
    1. Advertising

  3. On Tue, 10 Apr 2007 10:53:51 +0200, Elvandar wrote:
    > Java_it_ribes_serialDriver_SerialPort_nativeOpenPort (JNIEnv *env,
    > jobject jobj, jstring port)
    > {
    > LPSERIALPORT SerialPort = (LPSERIALPORT)malloc(sizeof(SERIALPORT));


    The above can't be right... SerialPort is local to this method and not
    stored elsewhere. How do you expect to find it later? Please post
    *real* code.

    In your callback method, have you confirmed that SerialPort != NULL,
    (see prev), that (javaEvent != 0), etc, i.e. that you actually *reach*
    CallVoidMethod()?

    If so, what does CallVoidMethod() return? What do
    env->ExceptionOccurred() and env->ExceptionDescribe() say?

    This might also help:
    http://groups.google.com/group/comp.lang.java.programmer/msg/511c52ef88a993c9

    /gordon

    --
     
    Gordon Beaton, Apr 10, 2007
    #3
  4. Elvandar

    Elvandar Guest

    Gordon Beaton was thinking very hard :
    > On Tue, 10 Apr 2007 10:53:51 +0200, Elvandar wrote:
    >> Java_it_ribes_serialDriver_SerialPort_nativeOpenPort (JNIEnv *env, jobject
    >> jobj, jstring port)
    >> {
    >> LPSERIALPORT SerialPort = (LPSERIALPORT)malloc(sizeof(SERIALPORT));

    >
    > The above can't be right... SerialPort is local to this method and not
    > stored elsewhere. How do you expect to find it later? Please post
    > *real* code.


    Sorry, I cleaned up a bit the code I posted because the function was
    too long, and I suppose I deleted a too much :p. In nativeOpenPort
    there was also the instruction "AddSerialPort(SerialPort);" that adds
    the object to a list of the current opened ports (see below), and
    retrieved in the notifyEvent method with the SearchSerialPort function.

    void AddSerialPort(LPSERIALPORT SerialPort)
    {
    LPPORTLIST newPort = (LPPORTLIST)malloc(sizeof(PORTLIST));

    newPort->ComPort = SerialPort;
    newPort->Next = openedPorts;
    openedPorts = newPort;
    }

    >
    > In your callback method, have you confirmed that SerialPort != NULL,
    > (see prev), that (javaEvent != 0), etc, i.e. that you actually *reach*
    > CallVoidMethod()?


    Yes

    >
    > If so, what does CallVoidMethod() return? What do
    > env->ExceptionOccurred() and env->ExceptionDescribe() say?
    >


    The java method sometimes seems to be correctly called (no exceptions
    thrown), but the data it retrieves is absolutely incorrect. The problem
    is that most of times it is not called, but if I open a serial terminal
    all data arrived and bufferized is correctly shown on screen...and if I
    run the same DLL in a java standalone app all works correctly, the
    problem only happens if I load the DLL under tomcat...

    > This might also help:
    > http://groups.google.com/group/comp.lang.java.programmer/msg/511c52ef88a993c9


    Seen it. My jar library is located under %CATALINA_HOME%\shared\lib,
    and in my tests (just to avoid problems of this kind) I restarted
    tomcat every time.

    Thanks for your help.
     
    Elvandar, Apr 10, 2007
    #4
  5. On Tue, 10 Apr 2007 12:21:26 +0200, Elvandar wrote:
    > The java method sometimes seems to be correctly called (no
    > exceptions thrown), but the data it retrieves is absolutely
    > incorrect. The problem is that most of times it is not called, but
    > if I open a serial terminal all data arrived and bufferized is
    > correctly shown on screen...and if I run the same DLL in a java
    > standalone app all works correctly, the problem only happens if I
    > load the DLL under tomcat...


    Have you confirmed that the data is correct when it reaches the native
    callback method, before you call Java?

    Have you attempted hardcoding some test data there?

    You don't seem to be initializing all of the SerialPort fields.
    Remember that memory returned by malloc() is *not* zeroed, and the
    actual contents may differ in the different runtime environments.

    >> This might also help:

    > Seen it.


    Hmm, yes I see now that that was you too...

    /gordon

    --
     
    Gordon Beaton, Apr 10, 2007
    #5
  6. Elvandar

    Elvandar Guest

    Gordon Beaton wrote on 10/04/2007 :
    > On Tue, 10 Apr 2007 12:21:26 +0200, Elvandar wrote:
    >> The java method sometimes seems to be correctly called (no
    >> exceptions thrown), but the data it retrieves is absolutely
    >> incorrect. The problem is that most of times it is not called, but
    >> if I open a serial terminal all data arrived and bufferized is
    >> correctly shown on screen...and if I run the same DLL in a java
    >> standalone app all works correctly, the problem only happens if I
    >> load the DLL under tomcat...

    >
    > Have you confirmed that the data is correct when it reaches the native
    > callback method, before you call Java?
    >
    > Have you attempted hardcoding some test data there?
    >


    I find out the problem is not located in my code and/or in something
    linked to tomcat. Even if in standalone applications all works well,
    the problem is linked with the GSM modem I'm currently using (a Siemens
    M20). With other modems (also of the Siemens family, like the TC35) or
    simulating a modem on a loopback link, all works well.

    Thanks for your help, I'll try to check why the M20 is so hard to
    handle :)
     
    Elvandar, Apr 10, 2007
    #6
  7. Elvandar

    Chris Uppal Guest

    Elvandar wrote:

    > The java method sometimes seems to be correctly called (no exceptions
    > thrown), but the data it retrieves is absolutely incorrect. The problem
    > is that most of times it is not called, but if I open a serial terminal
    > all data arrived and bufferized is correctly shown on screen...and if I
    > run the same DLL in a java standalone app all works correctly, the
    > problem only happens if I load the DLL under tomcat...


    The only possibilities I can think of (that you haven't already ruled out) are
    that either the machine is running under a higher load with Tomcat (and so may
    be missing asynchronous notifications somewhere), or that the threading
    environment is different.

    I'm a little bit suspicious of the calls to AttachCurrentThread() and
    DetachCurrentThread() in your NotifyEvent(). If that is being called at
    serial port speeds, and if I'm right in thinking that AttachCurrentThread() is
    a relatively heavyweight operation when the thread is not already attached[*],
    then that /might/ a source of problems.

    -- chris

    [*] AFAIK it involves messing around with the target thread's stack.
     
    Chris Uppal, Apr 10, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    851
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    906
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,702
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    623
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    815
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page