001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.samples;
019    
020    import java.util.Date;
021    
022    import org.otfeed.IConnection;
023    import org.otfeed.IRequest;
024    import org.otfeed.command.TickStreamCommand;
025    import org.otfeed.event.ICompletionDelegate;
026    import org.otfeed.event.IDataDelegate;
027    import org.otfeed.event.OTBBO;
028    import org.otfeed.event.OTError;
029    import org.otfeed.event.OTMMQuote;
030    import org.otfeed.event.OTQuote;
031    import org.otfeed.event.OTTrade;
032    
033    /**
034     * Sample application: requests real-time tick stream.
035     */
036    public class TickStream {
037            
038            public static void main(String... args) throws Exception {
039    
040                    if (args.length != 2) {
041                            System.out.println("USAGE: Feed <exchangeName> <symbolName>");
042                            System.exit(-1);
043                    }
044    
045                    System.out.println("# generated by " + TickStream.class + " on "
046                                    + FormatUtil.DATE_FORMAT_AS_STRING.format(new Date()));
047                    System.out.println("#");
048                    System.out.println("# tick stream for: " + args[0] + "/" + args[1]);
049                    System.out.println("#");
050    
051                    TickStreamCommand command = new TickStreamCommand();
052                    command.setExchangeCode(args[0]);
053                    command.setSymbolCode(args[1]);
054                    command.setQuoteDelegate(new IDataDelegate<OTQuote>() {
055                            public void onData(OTQuote data) {
056                                    System.out.println(data);
057                            }
058                    });
059                    command.setTradeDelegate(new IDataDelegate<OTTrade>() {
060                            public void onData(OTTrade data) {
061                                    System.out.println(data);
062                            }
063                    });
064                    command.setMmQuoteDelegate(new IDataDelegate<OTMMQuote>() {
065                            public void onData(OTMMQuote data) {
066                                    System.out.println(data);
067                            }
068                    });
069                    command.setBboDelegate(new IDataDelegate<OTBBO>() {
070                            public void onData(OTBBO data) {
071                                    System.out.println(data);
072                            }
073                    });
074    
075                    command.setCompletionDelegate(new ICompletionDelegate() {
076                            public void onDataEnd(OTError error) {
077                                    if(error != null) {
078                                            System.out.println("# Error: " + error);
079                                    }
080                            }
081                    });
082                    
083                    IConnection connection = Connector.connect();
084    
085                    try {
086                            IRequest request = connection.prepareRequest(command);
087                            request.submit();
088    
089                            request.waitForCompletion();
090    
091                    } finally {
092    
093                            connection.shutdown();
094                            connection.waitForCompletion();
095                    }
096            }
097    }