View Javadoc
1   package org.oxerr.huobi.fix;
2   
3   import java.util.concurrent.ExecutorService;
4   import java.util.concurrent.Executors;
5   
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import quickfix.Application;
10  import quickfix.DoNotSend;
11  import quickfix.FieldNotFound;
12  import quickfix.IncorrectDataFormat;
13  import quickfix.IncorrectTagValue;
14  import quickfix.Message;
15  import quickfix.RejectLogon;
16  import quickfix.Session;
17  import quickfix.SessionID;
18  import quickfix.UnsupportedMessageType;
19  import quickfix.fix44.MessageCracker;
20  
21  /**
22   * Abstract {@link Application} for Huobi.
23   */
24  public abstract class HuobiApplication extends MessageCracker implements
25  		Application {
26  
27  	private final Logger log = LoggerFactory.getLogger(HuobiApplication.class);
28  	private final ExecutorService executorService;
29  
30  	public HuobiApplication() {
31  		executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
32  				.availableProcessors() * 2 + 1);
33  	}
34  
35  	/**
36  	 * {@inheritDoc}
37  	 */
38  	@Override
39  	public void onCreate(SessionID sessionId) {
40  	}
41  
42  	/**
43  	 * {@inheritDoc}
44  	 */
45  	@Override
46  	public void onLogon(SessionID sessionId) {
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	@Override
53  	public void onLogout(SessionID sessionId) {
54  	}
55  
56  	/**
57  	 * {@inheritDoc}
58  	 */
59  	@Override
60  	public void toAdmin(Message message, SessionID sessionId) {
61  		log.trace("toAdmin: {}", message);
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Override
68  	public void fromAdmin(Message message, SessionID sessionId)
69  			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
70  			RejectLogon {
71  		log.trace("fromAdmin: {}", message);
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	@Override
78  	public void toApp(Message message, SessionID sessionId)
79  			throws DoNotSend {
80  		log.trace("toApp: {}", message);
81  	}
82  
83  	/**
84  	 * {@inheritDoc}
85  	 */
86  	@Override
87  	public void fromApp(Message message, SessionID sessionId)
88  			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
89  			UnsupportedMessageType {
90  		log.trace("fromApp: {}", message);
91  		crack(message, sessionId);
92  	}
93  
94  	public void sendMessage(final Message message, final SessionID sessionId) {
95  		log.trace("sending message: {}", message);
96  
97  		executorService.execute(new Runnable() {
98  
99  			@Override
100 			public void run() {
101 				Session.lookupSession(sessionId).send(message);
102 			}
103 
104 		});
105 	}
106 
107 }