Archived

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

bidir process pending,please help

I modified the example code "Ice/bidir" to adding a method :
sendMessage(String ruid, String info)
For one client can send a message to another client throught callback.

CallbackSenderI.java
synchronized public void
    addClient(String userid,Ice.Identity ident, Ice.Current current)
    {
        System.out.println("adding client `" + _communicator.identityToString(ident) + "'" + "#" + userid);

        Ice.ObjectPrx base = current.con.createProxy(ident);
        CallbackReceiverPrx cbr = CallbackReceiverPrxHelper.uncheckedCast(base);
        
        BeanClient client = new BeanClient();
        client.setUserId(userid);
        client.setCbrPrx(cbr);
        _clients.add(client);
    }

synchronized public int sendMessage(String ruid, String info, Ice.Current current) {

    	java.util.List<BeanClient> clients;
    	int ret = -1;
    	
    	synchronized(this)
        {
            clients = new java.util.ArrayList<BeanClient>(_clients);
        }
    	
    	if(!clients.isEmpty())
        {
            for(BeanClient p : clients)
            {
                if(p.getUserId().equals(ruid)){
	            	try
	                {
	            		ret = 0;
	                    p.getCbrPrx().callback(info);
	                }
	                catch(Exception ex)
	                {
	                	ret = -2;
	                    System.out.println("removing client `" + _communicator.identityToString(p.getCbrPrx().ice_getIdentity()) +  "':");
	                    ex.printStackTrace();
	
	                    synchronized(this)
	                    {
	                        _clients.remove(p);
	                    }
	                }
	            	
	            	break;
                }
            }
        }
    	
		return ret;
	}


TestSend.java
public class TestSend extends Ice.Application
{
    public int
    run(String[] args)
    {
        if(args.length < 2)
        {
            System.err.println(appName() + ": SendUserId reciveUserId Info");
            return 1;
        }

        CallbackSenderPrx server = CallbackSenderPrxHelper.checkedCast(
            communicator().propertyToProxy("CallbackSender.Proxy"));
        if(server == null)
        {
            System.err.println("invalid proxy");
            return 1;
        }
        
        Ice.ObjectAdapter adapter = communicator().createObjectAdapter("");
        Ice.Identity ident = new Ice.Identity();
        ident.name = java.util.UUID.randomUUID().toString();
        ident.category = "";
        adapter.add(new CallbackReceiverI(), ident);
        adapter.activate();
        server.ice_getConnection().setAdapter(adapter);
        server.addClient(args[0],ident);

        int f = server.sendMessage(args[1], args[2]);
        
        System.out.println("ret:"+f);
        
        adapter.destroy();
        
        return 0;
    }

    public static void
    main(String[] args)
    {
        TestSend app = new TestSend();
        int status = app.main("Client", args, "app.config.client");
        System.exit(status);
    }
}

#java com.chenote.app.Server
#java com.chenote.app.Client 111111
#java com.chenote.app.Client 222222
#java com.chenote.app.Client 333333


the client 222222 can recieve message When i run :
com.chenote.app.TestSend 000000 111111 helloworld

Server and all client is pending after that,what wrong with my code ,thanks and sorry for my pool english.

Comments

  • sorry ..............

    the client 222222 can recieve message When i run :
    com.chenote.app.TestSend 000000 222222 helloworld
  • xdm
    xdm La Coruña, Spain
    Hi,

    The nested call in sendMessage causes a deadlock in the server, at that point there isn't any thread available to process the reply of the callback invocation.

    The simple solution is to increase the server thread pool size to avoid the deadlock
    #config.server
    Ice.ThreadPool.Server.Size=10
    

    To better understand this read Nested Invocations - Ice 3.5 - ZeroC, the documentation explain other possible solutions and scenarios.

    For details about thread pools configuration see Thread Pools - Ice 3.5 - ZeroC
  • thanks a lot!