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 应用程序之间的内部粘合剂。
LogonHandler 接收 HTTP 登录请求并尝试将用户登录到 QuickFix/j 应用程序会话。
QuickFix/j 正在向 FIX 服务器发送登录消息,此登录请求/响应是异步的。HTTP 登录请求当然是同步的。因此,我们必须等待 FIX 服务器的回复,然后才能从 HTTP 请求返回。我使用 CountDownLatch 和这个 UserSession 对象来做到这一点。
当我创建 QuickFix/j 会话对象时,我还创建了一个 UserSession 对象并将其添加到地图中(这发生在 LogonHandler 登录方法中)。
QuickFix/j 应用程序对象中有两个回调,toAdmin() 和 fromAdmin()。在 fromAdmin() 中,我检查消息是否是登录响应,如果是,我调用 UserSession 的方法来倒计时锁存器。在调试代码时,我看到 fromAdmin() 方法被命中,在 map 中找到 UserSession 对象并调用 countdown() 方法,并且 latch.getCount() 从 1 变为 0,但是 latch.await( ) UserSession await() 中的方法永远不会返回。它总是超时。