Archived

This forum has been archived. Please start a new discussion on GitHub.

strange problem while using slice defined object in Java client

Well, guru:

This problem drive me almost crazy. First I have such an defination:
dictionary<string, string> StringDict;

class Context
{
    void addString(string k, string v);
    
    StringDict dictString;
};

interface MyService
{
    void foo1();
    double foo2(Context ctx);
};

I implemented a server and client in C++, create a class ContextI to implement Context, and add it to the object factory.

Everything seems ok in C++, then I try to write a client in Java, the same idea, create a class in Java to implement Context, and add it to object factory:
public final class ContextI extends MyModule.Context
{
    public ContextI()
    {
    }
    
    public final void addString(String k, String v, Ice.Current current)
    {
        System.out.println("key = " + k);
        this.dictString.put(k, v);
    }
}

then in my client code:
MyModeul.ServicePrx svc = .....;
svc.foo1();

this works fine, then I create context:
MyModule.ServicePrx svc = .....;
MyModule.Context c = new MyImpl.ContextI();
svc.foo1();

this also works find, then I add some values into context:
MyModule.ServicePrx svc = .....;
MyModule.Context c = new MyImpl.ContextI();
c.addString("aaa", "bbb");
svc.foo1();

then it goes wrong, I have put a print message in server's implementation of foo1(), and this time, I cannot get that output, seems this invokation isn't dispatched to server.

I tried to directly operate on the member of context:
MyModule.ServicePrx svc = .....;
MyModule.Context c = new MyImpl.Context();
c.dictString.put("aaa", "bbb");
svc.foo1();

same problem. I was going to crazy, can anybody help me?

Comments

  • or any hints?
  • mes
    mes California
    If I understand your situation correctly, your client has created a local instance of ContextI and is invoking operations on it. These operations are dispatched by the local ContextI object. Notice that your client is invoking methods on a ContextI object, and not on a ContextPrx object. If you want these invocations to affect an instance of ContextI in the server, you must obtain a proxy for that object.

    Hope that helps,
    Mark
  • mes wrote: »
    If I understand your situation correctly, your client has created a local instance of ContextI and is invoking operations on it. These operations are dispatched by the local ContextI object. Notice that your client is invoking methods on a ContextI object, and not on a ContextPrx object. If you want these invocations to affect an instance of ContextI in the server, you must obtain a proxy for that object.

    Hope that helps,
    Mark

    Thanks for you reply, yes, I created a local object (the Ptr in C++), and I want to send this object to the remote server, not using Prx, that's why I add an object factory for this kind of object, the target interface I want to use is foo2, which will take Context as the argument, not Context*.

    In another word, I want to create a slice defined object (defined in class, which has operations and member data), and I didn't define it as a local class, this will cause the slice2java to generate the marshalling code for me, and when I send the object, it can be serilized, and re-created by the server's object factory.

    I didn't meet any problem in C++, everything works well.
  • dwayne
    dwayne St. John's, Newfoundland
    You are not initializing dictString in ContextI, which means it's value is null. Most likely this is throwing a NullPointerException when you attempt to use it which you must be catching and ignoring elsewhere. In C++ this is not an issue since by default the uninitialized dictSting member would just be an empty std::map rather than null.

    If this is not the problem please post small complete example that demonstrates the problem. That makes it much easier for us to help you.
  • The problem I met is, if I created a local object which is an implementation of a slice defined class, then the following remote invokation seems cannot be dispatched to the servent (I print out message in servent of foo1 and foo2), and it also won't report any exception. I did this in C++ and it works, but in Java it has this problem, any hints on what maybe wrong?
  • dwayne wrote: »
    You are not initializing dictString in ContextI, which means it's value is null. Most likely this is throwing a NullPointerException when you attempt to use it which you must be catching and ignoring elsewhere. In C++ this is not an issue since by default the uninitialized dictSting member would just be an empty std::map rather than null.

    If this is not the problem please post small complete example that demonstrates the problem. That makes it much easier for us to help you.

    Thanks, this might be the problem, cause Java variable is always pointer! thanks, I'll test it right now. For the example, I think I have describe it in a simple one:-) cause I have no idea on where it might wrong :-)

    any way, thanks very much.
  • dwayne wrote: »
    You are not initializing dictString in ContextI, which means it's value is null. Most likely this is throwing a NullPointerException when you attempt to use it which you must be catching and ignoring elsewhere. In C++ this is not an issue since by default the uninitialized dictSting member would just be an empty std::map rather than null.

    If this is not the problem please post small complete example that demonstrates the problem. That makes it much easier for us to help you.

    Thanks, I tested, this is the problem, you have to new a HashMap for each member defined in slice.... Sorry for this is such a stupid problem cause I use C++ a lot.

    Thanks again!