Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package net.phoenix.thrift.hello; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.nio.ByteBuffer; import net.phoenix.thrift.gen.Hello; import net.phoenix.thrift.gen.HelloService; import org.apache.commons.lang3.ArrayUtils; import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.junit.Test; /** * * @author Shamphone Lee<shamphone@gmail.com> * */ public class ThreadPoolTest extends ServerTestBase { private static final Logger LOG = Logger.getLogger(ThreadedSelectorTest.class); public ThreadPoolTest() { this.springConfigPath = "spring-hello-thread-pool.xml"; } @Test public void testSimple() throws TException, IOException, InterruptedException { LOG.info("Client starting...."); String name = "Hello "; long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { TTransport transport = new TSocket("localhost", 9804); // TTransport transport = new TFramedTransport(new // TSocket("localhost", 9804)); transport.open(); // TTransport transport = new TTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); try { assertEquals(name + " World", client.hello2(name)); // System.out.println(message); } finally { transport.close(); } } LOG.info("Request timer: " + (System.currentTimeMillis() - start)); } @Test public void testBinary() throws Exception { LOG.info("Client starting...."); String name = "Hello "; // TTransport transport = new TNonblockingSocket("localhost", 9804); TTransport transport = new TSocket("localhost", 9804); transport.open(); // TTransport transport = new TTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); try { ByteBuffer buffer = client.testBinary(ByteBuffer.wrap(name.getBytes("UTF-8"))); assertEquals(new String(buffer.array(), buffer.position(), buffer.limit() - buffer.position(), "UTF-8"), "Hello " + name); // System.out.println(message); } finally { transport.close(); } } @Test public void testByteBuffer() throws TException, IOException, InterruptedException { LOG.info("Client starting...."); String name = "Hello "; // TTransport transport = new TNonblockingSocket("localhost", 9804); TTransport transport = new TSocket("localhost", 9804); transport.open(); // TTransport transport = new TTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); try { // System.out.println("start request. "); Hello.HelloRequest.Builder request = Hello.HelloRequest.newBuilder(); request.setName(name); Hello.User.Builder user = Hello.User.newBuilder(); user.setName("hello"); user.setPassword("hello"); request.setUser(user.build()); ByteBuffer requestBuffer = ByteBuffer.wrap(request.build().toByteArray()); ByteBuffer buffer = client.hello(requestBuffer); Hello.HelloResponse response = Hello.HelloResponse .parseFrom(ArrayUtils.subarray(buffer.array(), buffer.position(), buffer.limit())); String message = response.getMessage(); assertEquals(message, "Hello " + name); // System.out.println(message); } finally { transport.close(); } } }