从Java中的方法返回之前,需要等待异步api回调 [英] Need to wait for asynchronous api callback before I return from method in Java

查看:199
本文介绍了从Java中的方法返回之前,需要等待异步api回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.util.concurrent.CountDownLatch;

  import quickfix.Initiator;


  public class UserSession {
    private final CountDownLatch latch = new CountDownLatch(1);

public String await() {
        try {
            System.out.println("waiting...");
            if (latch.await(5, TimeUnit.SECONDS))
                System.out.println("released!");
            else
                System.out.println("timed out");
            return secret;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public void countdown(String s) {
        System.out.println("In countdown: "+s+ ". Latch count: "+latch.getCount());
        secret = s;
        latch.countDown();
        System.out.println("Latch count: "+latch.getCount());
    }
  }


  public class LogonHandler extends AbstractHandler {

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException
        {
            Map<String,String[]> query = request.getParameterMap();

            if (query.containsKey("method")) {
                if (query.get("method")[0].compareTo(method) == 0) {
                    baseRequest.setHandled(true);
                    response.getWriter().println(logon(query));
                }
            }
            else
                baseRequest.setHandled(false);
        }

    private String logon(Map<String,String[]> query) {
        if (query.containsKey("username") && query.containsKey("password") &&           query.containsKey("sendercompid")) {

            app.mapUser(query.get("sendercompid")[0], new   UserSession(query.get("username")[0], query.get("password")[0]));

            SessionID session = new SessionID(new BeginString("FIX.4.4"), new SenderCompID(query.get("sendercompid")[0]), new TargetCompID("PARFX"));

            try {
                ThreadedSocketInitiator tsi = new ThreadedSocketInitiator(app, app.getFileStoreFactory(), settings, app.getLogFactory(), app.getMessageFactory());
                UserSession userSession = new UserSession(query.get("username")[0], query.get("password")[0]);
                userSession.setInitiator(tsi);

                tsi.start();
                return userSession.await();
            } catch (ConfigError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
            }
        }
        return "fail";
    }
  }


public class QuickfixjApplication implements Application {
    private Map<String,UserSession> users = new HashMap<String,UserSession>();

    public void mapUser(String s, UserSession u) {
        users.put(s, u);
    }

    public void toAdmin(Message message, SessionID sessionId) {

        try {
            if (message.getHeader().getField(new StringField(MsgType.FIELD)).valueEquals(Logon.MSGTYPE)) {
                UserSession user = users.get(sessionId.getSenderCompID());
                message.setField(new Username(user.getUsername()));
                message.setField(new Password(user.getPassword()));
            }
        } catch (FieldNotFound e) {
            e.printStackTrace();
        }
    }

    public void fromAdmin(Message message, SessionID sessionId)
        throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon {

        if (message.getHeader().getField(new StringField(MsgType.FIELD)).valueEquals(Logon.MSGTYPE)) {
            System.out.println(message.toString());
            UserSession user = users.get(sessionId.getSenderCompID());
            user.countdown(message.toString());
        }
    }
}

好的,我尝试只在此处包括最少的代码.有三个有趣的类,UserSession是Jetty处理程序和QuickFix/j应用程序之间的内部粘合剂.

Ok, I've tried to only include the minimum amount of code here. There are three interesting classes, UserSession is the internal glue between the Jetty handler and the QuickFix/j application.

LogonHandler接收HTTP登录请求,并尝试将用户登录到QuickFix/j应用程序会话.

The LogonHandler receives an HTTP logon request and tries to log a user onto a QuickFix/j application session.

QuickFix/j正在向FIX服务器发送登录消息,该登录请求/响应是异步的. HTTP登录请求当然是同步的.因此,我们必须等待FIX服务器的回复,然后才能从HTTP请求返回.我使用CountDownLatch和此UserSession对象进行此操作.

QuickFix/j is sending a logon message to a FIX server, this logon request / response is asynchronous. The HTTP logon request is of course synchronous. So we have to wait for the reply from the FIX server before we return from the HTTP request. I do this using CountDownLatch and this UserSession object.

当我创建QuickFix/j会话对象时,我还创建了一个UserSession对象,并将其添加到地图中(这发生在LogonHandler登录方法中).

When I create the QuickFix/j session object I also create a UserSession object and add it to a map (that happens in the LogonHandler logon method).

QuickFix/j应用程序对象中有两个回调,分别为toAdmin()和fromAdmin().在fromAdmin()中,我检查消息是否为登录响应,如果是,则调用UserSession的方法对闩锁进行倒计时.在调试代码时,我看到点击了fromAdmin()方法,在映射中找到了UserSession对象,并调用了countdown()方法,latch.getCount()从1变为0,但是latch.await( )UserSession await()中的方法永远不会返回.它总是超时.

There are two callbacks in the QuickFix/j application object, toAdmin() and fromAdmin(). In fromAdmin() I check if the message is a logon response and if it is I call a method of UserSession to countdown the latch. In debugging the code I see that the fromAdmin() method is hit, the UserSession object is found in the map and the countdown() method is called and the latch.getCount() goes from 1 to 0, but the latch.await() method in UserSession await() never returns. It always times out.

推荐答案

您可以像这样使用CountDownLatch:

public class LogonHandler implements Handler {
    private final CountDownLatch loginLatch = new CountDownLatch (1);

    private boolean callbackResults;

    public void serverResponseCallback(boolean result) {
        callbackResults = result;
        loginLatch.countDown ();
    }

    public boolean tryLogon(Credentials creds) throws InterruptedException {
        SomeServer server = new SomeServer(address);
        server.tryLogon (creds.getName (), creds.getPass ());
        loginLatch.await ();
        return callbackResults;
    }
}

如果要将等待时间限制为例如5秒,请使用以下命令代替loginLatch.await ():

If you want to limit waiting time by, for example, 5 seconds, then instead of loginLatch.await () use the following:

if (loginLatch.await (5L, TimeUnit.SECONDS))
    return callbackResults;
else
    return false; // Timeout exceeded

这篇关于从Java中的方法返回之前,需要等待异步api回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆