users@glassfish.java.net

Re: How to invoke Client Callback Handler from EJB3 Session Bean

From: Miroslav Nachev <miro_at_space-comm.com>
Date: Sun, 18 May 2008 19:21:30 +0300
Hi,

Today I try to implement such slient side callback mechanism as your suggestions but the result was that the computer blocks. Here is the code:
Client side
public class ClientCallback
    extends Thread
{
    private boolean works;

    @Override
    public void run()
    {
        works = true;
        ClientCallbackRemote cc;
        List<Callback> callbacks = null;
        List<Callback> callbacksResult = null;
        try
        {
            cc = InitialContext.doLookup(ClientCallbackRemote.class.getName());
        }
        catch(Exception ex)
        {
            throw new RuntimeException(ex);
        }

        while(works)
        {
            callbacks = cc.clientHandle(callbacksResult);
            if(callbacks != null && callbacks.size() > 0)
            {
                // Do something
                ((ChoiceCallback)callbacks.get(0)).setSelectedIndex(7);

                callbacksResult = callbacks;
            }
        }

        System.out.println("ClientCallback stoped.");
    }

    public boolean isWorks() {
        return works;
    }

    public void setWorks(boolean works) {
        this.works = works;
    }
}


Server Side
@Local
public interface ClientCallbackLocal
    extends CallbackHandler
{
}

@Remote
public interface ClientCallbackRemote {
    List<Callback> clientHandle(List<Callback> callbacksResult);
}

@Stateless
public class ClientCallbackBean
    implements  ClientCallbackRemote,
                ClientCallbackLocal,
                CallbackHandler
{
    private boolean newCallbackRequest;

    private List<Callback> callbacksResult;
    private boolean hasCallbacksResult;

    private List<Callback> callbacks;

    public List<Callback> clientHandle(List<Callback> callbacksResult)
    {
        System.out.println("clientHandle.callbacksResult: " + callbacksResult);
        this.callbacksResult = callbacksResult;
        hasCallbacksResult = true;

        while(!newCallbackRequest) {}

        newCallbackRequest = false;
        System.out.println("clientHandle.newCallbacks: " + callbacks);
        return callbacks;
    }

    public synchronized void handle(Callback[] callbacks)
        throws IOException, UnsupportedCallbackException
    {
        System.out.println("handle.callbacks: " + callbacks);
        this.callbacks = Arrays.asList(callbacks);
        newCallbackRequest = true;

        hasCallbacksResult = false;
        while(!hasCallbacksResult) {}
        hasCallbacksResult = false;

        int size;
        if(callbacksResult != null && (size = callbacksResult.size()) > 0)
        {
            for(int i = 0; i < size; i++)
            {
                callbacks[i] = callbacksResult.get(i);
            }
        }
        System.out.println("handle.callbacksResult: " + callbacksResult);
    }
}

Any suggestions how to improve it?


Regards,
Miro.


glassfish@javadesktop.org wrote:
Miro,

Everyone who has replied to you is trying to help, although you seem to be frustrated with what we are saying.  Let me try to explain once again.

As several of us have written, the Java EE programming model does not support what you want to do using a simple method invocation model.  

You have mentioned the client authentication callback several times, repeatedly asking for what classes and methods in that implementation you can use to do what you want.  You have assumed that the ORB server calls back to the ORB client.  It does not.  

In fact the naming and ORB implementation internally follows exactly the approach ljnelson and I have suggested that you use in your application.  The fact that authentication is required is returned to the ORB client as part of a remote method [i]return value[/i]. The ORB on the client side uses that return value to prompt the user for additional information using the authentication callback.  (Note that this method callback occurs locally, from one object on the client to another.  It does NOT occur from the server to the client.)  Then the ORB sends another remote method call to the server with the new information collected from the user.  Always the ORB client makes the method call and responds to the return value.

ljnelson and I have suggested one way you can solve your problem using a similar approach.  Markus suggested another. I understand that neither of these approaches is what you were thinking about initially, and neither is as simple as the remote method callback you first talked about.  But your approach is not supported.  No one will be able to tell you what class and method in the ORB implementation you can use to do this because no such method exists that will do what you want the way you want to do it.  Plus it would not be supported and I would think you want to avoid an unsupported solution. 

I believe that the technique ljnelson and I have suggested would be only slightly more complicated then your original idea in which the server calls back to the client.  In your approach the server might be able to invoke one of several different client-side methods, depending on what additional information might be needed.  In ljnelson's and my approach, the server needs to return that information as part of a return value object from the EJB method so the client can figure out what information to request of the user.  

I understand you are frustrated that you cannot solve the problem in the way you first thought would work.  People have tried to be helpful, suggesting several different ways you can implement your application that will meet the business need using what Java EE offers.  I hope you will be able to choose one of those that meets the business need - even if it is not the technical approach you first had in mind - and be successful with your application.

- Tim
[Message sent by forum member 'tjquinn' (tjquinn)]

http://forums.java.net/jive/thread.jspa?messageID=275054

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net