001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package backtype.storm.spout;
031
032import java.io.UnsupportedEncodingException;
033import java.nio.ByteBuffer;
034import java.util.ArrayList;
035import java.util.List;
036import java.util.Set;
037
038import net.lag.kestrel.thrift.Item;
039import net.lag.kestrel.thrift.Kestrel;
040import net.lag.kestrel.thrift.QueueInfo;
041
042import org.apache.thrift7.TException;
043import org.apache.thrift7.protocol.TBinaryProtocol;
044import org.apache.thrift7.protocol.TProtocol;
045import org.apache.thrift7.transport.TFramedTransport;
046import org.apache.thrift7.transport.TSocket;
047import org.apache.thrift7.transport.TTransport;
048
049/* Thin wrapper around Thrift Client for Kestrel */
050public class KestrelThriftClient implements Kestrel.Iface {
051        Kestrel.Client _client = null;
052        TTransport _transport = null;
053
054        public KestrelThriftClient(String hostname, int port)
055                        throws TException
056        {
057
058                _transport = new TFramedTransport(new TSocket(hostname, port));
059                final TProtocol proto = new TBinaryProtocol(_transport);
060                _client = new Kestrel.Client(proto);
061                _transport.open();
062        }
063
064        public void close() {
065                _transport.close();
066                _transport = null;
067                _client = null;
068        }
069
070        @Override
071        public QueueInfo peek(String queue_name) throws TException {
072                return _client.peek(queue_name);
073        }
074
075        @Override
076        public void delete_queue(String queue_name) throws TException {
077                _client.delete_queue(queue_name);
078        }
079
080        @Override
081        public String get_version() throws TException {
082                return _client.get_version();
083        }
084
085        @Override
086        public int put(String queue_name, List<ByteBuffer> items, int expiration_msec) throws TException {
087                return _client.put(queue_name, items, expiration_msec);
088        }
089
090        public void put(String queue_name, String item, int expiration_msec) throws TException {
091                final List<ByteBuffer> toPut = new ArrayList<ByteBuffer>();
092                try {
093                        toPut.add(ByteBuffer.wrap(item.getBytes("UTF-8")));
094                } catch (final UnsupportedEncodingException e) {
095                        throw new RuntimeException(e);
096                }
097                put(queue_name, toPut, expiration_msec);
098        }
099
100        @Override
101        public List<Item> get(String queue_name, int max_items, int timeout_msec, int auto_abort_msec) throws TException {
102                return _client.get(queue_name, max_items, timeout_msec, auto_abort_msec);
103        }
104
105        @Override
106        public int confirm(String queue_name, Set<Long> ids) throws TException {
107                return _client.confirm(queue_name, ids);
108        }
109
110        @Override
111        public int abort(String queue_name, Set<Long> ids) throws TException {
112                return _client.abort(queue_name, ids);
113        }
114
115        @Override
116        public void flush_queue(String queue_name) throws TException {
117                _client.flush_queue(queue_name);
118        }
119
120        @Override
121        public void flush_all_queues() throws TException {
122                _client.flush_all_queues();
123        }
124
125}