HttpAsyncClient PoolingNHttpClientConnectionManager.requestConnection failed to return? - java

Following is the code to request NHttpClientConnection from PoolingNHttpClientConnectionManager. The call connFuture.get(), fails to return. Anyone knows why? I am using HttpAsyncClient library httpasyncclient-4.0.1.jar
static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(100);
long connectTimeout=1;
long leaseTimeout=4;
TimeUnit timeUnit = TimeUnit.SECONDS;
Object state = null;
HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit,
new FutureCallback<NHttpClientConnection>() {
public void completed(final NHttpClientConnection c) {
System.out.println("completed");
httpConn = c;
}
public void failed(final Exception ex) {
System.out.println("failed");
}
public void cancelled() {
System.out.println("cancelled");
}
} );
System.out.println("Step3");
connFuture.get(); // Failed to return
System.out.println("Done");
}

I got it. ioReactor needs to be started. Here is the code that works.
static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
// Create client-side I/O event dispatch
final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(100);
long connectTimeout=1;
long leaseTimeout=4;
TimeUnit timeUnit = TimeUnit.SECONDS;
Object state = null;
//HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
HttpRoute route = new HttpRoute(new HttpHost("www.google.com"));
// Run the I/O reactor in a separate thread
Thread t = new Thread(new Runnable() {
public void run() {
try {
// Ready to go!
ioReactor.execute(ioEventDispatch);
} catch (InterruptedIOException ex) {
System.err.println("Interrupted");
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
}
System.out.println("Shutdown");
}
});
t.start();
Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit,
new FutureCallback<NHttpClientConnection>() {
public void completed(final NHttpClientConnection c) {
System.out.println("completed");
httpConn = c;
}
public void failed(final Exception ex) {
System.out.println("failed");
}
public void cancelled() {
System.out.println("cancelled");
}
} );
System.out.println("Step3");
connFuture.get();
System.out.println("Done");
ioReactor.shutdown();
}

Related

Why I can't receive data by a socket

I can send data in python and received in server, but after sleep more than 1ms(if do some For-loops(E.g: for i in range(0, 60):
print i), the result is same), I use socket to send data, but can't be received in java server.
There are code:
A client is writen in python2:
address = ('127.0.0.1', 9898)
ccc = socket(AF_INET, SOCK_STREAM)
ccc.connect(address)
ccc.send("client" + str(1) + ":before time.sleep send1111\n")
time.sleep(0.001)
ccc.send("client2:222222 after time.sleep\n")# if sleep 0.0009,can received,but 0.001 can't
A server is written in java:
ReceiveListener listener = new ReceiveListener() {
#Override
public void onReceived(int clientId, String msg) {
System.out.println(clientId + ":" + msg);
}
};
ClientManager clientManager = ClientManager.getInstance(listener, 9898);
clientManager.start();
And this is class manage the client:
public class ClientManager {
private static ServerThread serverThread = null;
private static ClientManager instance = null;
private final int port;
private ReceiveListener receiveListener = null;
private ClientManager(ReceiveListener receiveListener, int port) {
this.receiveListener = receiveListener;
this.port = port;
serverThread = new ServerThread(receiveListener, port);
}
public static ClientManager getInstance(ReceiveListener receiveListener, int port) {
if (instance == null) {
synchronized (ClientManager.class) {
if (instance == null) {
instance = new ClientManager(receiveListener, port);
}
}
}
return instance;
}
public void stop() {
serverThread.Stop();
serverThread = null;
}
public void start() {
if (serverThread == null) {
serverThread = new ServerThread(receiveListener, port);
}
new Thread(serverThread).start();
}
public static class ServerThread implements Runnable {
private ReceiveListener receiveListener;
private static Map<Integer, HandleMsgTask> tasks = new HashMap<>();
private final AtomicBoolean isExit = new AtomicBoolean(false);
private ServerSocket server;
int i = 0;
public ServerThread(ReceiveListener receiveListener, int port) {
try {
this.receiveListener = receiveListener;
this.server = new ServerSocket(port);
} catch (IOException e) {
System.out.println("failed:" + e.getMessage());
}
}
#Override
public void run() {
try {
while (!isExit.get()) {
System.out.println("wait devices... ... " + i);
Socket client = server.accept();
HandleMsgTask task = new HandleMsgTask(i, client, receiveListener);
new Thread(task).start();
tasks.put(i, task);
i++;
System.out.println("No:" + i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static class HandleMsgTask implements Runnable {
public final int clientId;
public final Socket client;
public static boolean connectStop = false;
private final ReceiveListener ReceiveListener;
public HandleMsgTask(int i, Socket client, ReceiveListener ReceiveListener) {
this.clientId = i;
this.client = client;
this.ReceiveListener = ReceiveListener;
}
public void disconnectClient() {
connectStop = true;
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
try {
final String address = client.getRemoteSocketAddress().toString();
System.out.println(clientId + ":" + address);
InputStream inputStream = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (reader.ready() && !connectStop) {
String line = reader.readLine();
if (ReceiveListener != null) {
ReceiveListener.onReceived(clientId, line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void Stop() {
if (tasks != null) {
for (HandleMsgTask task : tasks.values()) {
task.disconnectClient();
}
tasks.clear();
}
isExit.set(true);
if (server != null) {
try {
server.close();
System.out.println("close server");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Could someone help me? Thanks!
As user207421 said,I misused ready(); I Change the code bellow and solve the problem:
edit:
while (!bIsStopped.get() && ((line = reader.readLine()) != null)) {
if (newMsgRecListener != null) {
newMsgRecListener.onMsgRec(clientId, line);
}
}

Executor Service invokeAll

I am fairly new to the callable interface. I have some code which I can't get to compile at the moment and just need some help on why....
public List<String> getNonPingableRegisters (Collection<RegisterReplicationSynchTime> nonReplicatingRegisters) throws IOException {
int nThreads = 15;
final ExecutorService es = Executors.newFixedThreadPool(nThreads);
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
for (RegisterReplicationSynchTime nonReplicatingRegister : nonReplicatingRegisters) {
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
}
List<Future<String>> taskResults = es.invokeAll(pingTasks);
List<String> results = new ArrayList<String>();
for (Future<String> taskResult : taskResults) {
try {
String output = taskResult.get();
if (StringUtils.isNotEmpty(output) ) {
results.add(output);
}
} catch (InterruptedException e) {
// handle accordingly
} catch (ExecutionException e) {
// handle accordingly
}
}
return results;
}
Where PingTask is ...
public class PingTask implements Callable<String> {
private String hostname = null;
public PingTask(String hostname) {
this.hostname = hostname;
}
public String call() {
Socket socket = null;
boolean reachable = false;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(hostname, 139), 1000); //1 sec timeout
reachable = true;
socket.close();
} catch (IOException e) {
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return (reachable ? "" : hostname);
}
}
The compile error is at ...
List<Future<String>> taskResults = es.invokeAll(pingTasks);
The method add(Callable) in the type Collection> is not applicable for the arguments (PingTask) StoreReplicationSynchtimeManagerImpl.java
Not sure what I need to do here to make the call to invokeAll. Would appreciate some help.
thanks
Error is not at that line.
It's at:
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
Your collection is of Callable where as your PingTask class implements Callable. Change the collection to:
Collection<Callable<String>>
Here's your mistake:
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
PingTask implements Callable<String>, not Callable<PingTask>. You need to declare your list as Collection<PingTask> or Collection<Callable<String>>.
There is a type mis-match.
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>
But PingTask is declared as
public class PingTask implements Callable<String>
Change collection as Collection<PingTask>
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
will cause compile time error due to Callable<String> type addition

How can I use TBinaryProtocol and TFramedTransport in Java for Async server and client?

How can I use TBinaryProtocol and TFramedTransport in Java for Async server and client? Lot of the examples I get form web just fail and the documentation is not great. Any help with concrete example would be great!
public class NonblockingServer {
private void start() {
try {
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TThreadPoolServer.Args serverArgs = newTThreadPoolServer.Args(serverTransport);
serverArgs.processor(processor);
serverArgs.protocolFactory(protocolFactory);
TServer server = new TThreadPoolServer(serverArgs)
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NonblockingServer srv = new NonblockingServer();
srv.start();
}
}
AsyncClient
public class AsyncClient {
private void invoke() {
try {
ArithmeticService.AsyncClient client = new ArithmeticService.
AsyncClient(new TBinaryProtocol.Factory(), new TAsyncClientManager(),
new TNonblockingSocket("localhost", 7911));
client.add(200, 400, new AddMethodCallback());
client = new ArithmeticService.
AsyncClient(new TBinaryProtocol.Factory(), new TAsyncClientManager(),
new TNonblockingSocket("localhost", 7911));
client.multiply(20, 50, new MultiplyMethodCallback());
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AsyncClient c = new AsyncClient();
c.invoke();
}
I get the following exception Error :
java.io.IOException: Read call frame size failed
at org.apache.thrift.async.TAsyncMethodCall.doReadingResponseSize(TAsyncMethodCall.java:234)
at org.apache.thrift.async.TAsyncMethodCall.transition(TAsyncMethodCall.java:192)
at org.apache.thrift.async.TAsyncClientManager$SelectThread.transitionMethods(TAsyncClientManager.java:143)
at org.apache.thrift.async.TAsyncClientManager$SelectThread.run(TAsyncClientManager.java:113)
The Error seem occur here in the Thrift Source code
private void doReadingResponseSize() throws IOException {
if (transport.read(sizeBuffer) < 0) {
throw new IOException("Read call frame size failed");
}
if (sizeBuffer.remaining() == 0) {
state = State.READING_RESPONSE_BODY;
frameBuffer = ByteBuffer.allocate(TFramedTransport.decodeFrameSize(sizeBufferArray));
}
}

MVEL executeExpression function cannot be concurrent

Run the main function in File2 , the problem is : threads stuck at "rval=MVEL.executeExpression(compiledExpression, vars);" , 10 threads run in sequential order, not parallel , I wanna know why this happened.
PS: I'm using MVEL 2.2 , the latest version
File1:MVELHelper.java
public class MVELHelper {
private static ParserContext _ctx = new ParserContext(false);
//public static Object execute(String expression, Map<String, Object> vars, Databus databus) throws Exception {
public static Object execute(String expression, Map<String, Object> vars) throws Exception {
Object rval = null;
try {
if(vars == null) {
rval = MVEL.eval(expression, new HashMap<String,Object>());
}
else {
rval = MVEL.eval(expression, vars);
}
return rval;
}
catch(Exception e) {
throw new Exception("MVEL FAILED:"+expression,e);
}
}
public static Serializable compile(String text, ParserContext ctx)
throws Exception {
if(ctx == null) {
//ctx = _ctx;
ctx=new ParserContext(false);
}
Serializable exp = null;
try {
exp = MVEL.compileExpression(text, ctx);
//exp = MVEL.compileExpression(text);
}
catch (Exception e) {
throw new Exception("failed to compile expression.", e);
}
return exp;
}
public static Object compileAndExecute(String expression, Map<String, Object> vars) throws Exception {
Object rval = null;
try {
Serializable compiledExpression=compile(expression,null);
System.out.println("[COMPILE OVER, Thread Id="+Thread.currentThread().getId()+"] ");
if(vars == null) {
rval=MVEL.executeExpression(compiledExpression, new HashMap<String,Object>());
//rval = MVEL.eval(exp, new HashMap<String,Object>());
}
else {
//rval=MVEL.executeExpression(compiledExpression, vars,(VariableResolverFactory)null);
rval=MVEL.executeExpression(compiledExpression, vars);
//rval = MVEL.eval(expression, vars);
}
return rval;
}
catch(Exception e) {
throw new Exception("MVEL FAILED:"+expression,e);
}
}
}
File2:ExecThread3.java
public class ExecThread3 implements Runnable{
Map dataMap=null;
public Map getDataMap() {
return dataMap;
}
public void setDataMap(Map dataMap) {
this.dataMap = dataMap;
}
#Override
public void run() {
Map varsMap = new HashMap();
Map dataMap=new HashMap();
dataMap.put("count",100);
varsMap.put("dataMap", dataMap);
String expression="System.out.println(\"[BEFORE Thread Id=\"+Thread.currentThread().getId()+\"] \"+dataMap.get(\"count\"));"+
"Thread.sleep(3000);"+
"System.err.println(\"[AFTER Thread Id=\"+Thread.currentThread().getId()+\"] \"+dataMap.get(\"count\"));";
try {
//MVEL.compileExpression(expression);
MVELHelper.compileAndExecute(expression, varsMap);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int k=0;k<10;k++){
ExecThread3 execThread=new ExecThread3();
new Thread(execThread).start();
}
}
}

How to manipulate Message coming from Netty server/client

I am prototyping a Netty client/server transfer for strings, now I want to pass these strings to file when it arrives to server side.
Client:
private ClientBootstrap bootstrap;
private Channel connector;
private MyHandler handler=new MyHandler();
public boolean start() {
// Standard netty bootstrapping stuff.
Executor bossPool = Executors.newCachedThreadPool();
Executor workerPool = Executors.newCachedThreadPool();
ChannelFactory factory =
new NioClientSocketChannelFactory(bossPool, workerPool);
this.bootstrap = new ClientBootstrap(factory);
// Declared outside to fit under 80 char limit
final DelimiterBasedFrameDecoder frameDecoder =
new DelimiterBasedFrameDecoder(Integer.MAX_VALUE,
Delimiters.lineDelimiter());
this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
handler,
frameDecoder,
new StringDecoder(),
new StringEncoder());
}
});
ChannelFuture future = this.bootstrap
.connect(new InetSocketAddress("localhost", 12345));
if (!future.awaitUninterruptibly().isSuccess()) {
System.out.println("--- CLIENT - Failed to connect to server at " +
"localhost:12345.");
this.bootstrap.releaseExternalResources();
return false;
}
this.connector = future.getChannel();
return this.connector.isConnected();
}
public void stop() {
if (this.connector != null) {
this.connector.close().awaitUninterruptibly();
}
this.bootstrap.releaseExternalResources();
System.out.println("--- CLIENT - Stopped.");
}
public boolean sendMessage(String message) {
if (this.connector.isConnected()) {
// Append \n if it's not present, because of the frame delimiter
if (!message.endsWith("\n")) {
this.connector.write(message + '\n');
} else {
this.connector.write(message);
}
System.out.print(message);
return true;
}
return false;
}
Server:
private final String id;
private ServerBootstrap bootstrap;
private ChannelGroup channelGroup;
private MyHandler handler= new MyHandler();
public Server(String id) {
this.id = id;
}
// public methods ---------------------------------------------------------
public boolean start() {
// Pretty standard Netty startup stuff...
// boss/worker executors, channel factory, channel group, pipeline, ...
Executor bossPool = Executors.newCachedThreadPool();
Executor workerPool = Executors.newCachedThreadPool();
ChannelFactory factory =
new NioServerSocketChannelFactory(bossPool, workerPool);
this.bootstrap = new ServerBootstrap(factory);
this.channelGroup = new DefaultChannelGroup(this.id + "-all-channels");
// declared here to fit under the 80 char limit
final ChannelHandler delimiter =
new DelimiterBasedFrameDecoder(Integer.MAX_VALUE,
Delimiters.lineDelimiter());
this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
#Override
public ChannelPipeline getPipeline() throws Exception {
SimpleChannelHandler handshakeHandler =
new SimpleChannelHandler();
return Channels.pipeline(
handler,
delimiter,
new StringDecoder(),
new StringEncoder(),
handshakeHandler);
}
});
Channel acceptor = this.bootstrap.bind(new InetSocketAddress(12345));
if (acceptor.isBound()) {
System.out.println("+++ SERVER - bound to *:12345");
this.channelGroup.add(acceptor);
return true;
} else {
System.err.println("+++ SERVER - Failed to bind to *:12345");
this.bootstrap.releaseExternalResources();
return false;
}
}
public void stop() {
this.channelGroup.close().awaitUninterruptibly();
this.bootstrap.releaseExternalResources();
System.err.println("+++ SERVER - Stopped.");
}
Handlers used:
Client handler:
public class MyHandler extends SimpleChannelUpstreamHandler{
#Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
if(e.getMessage() instanceof String){
System.out.println((String)e.getMessage());
}
System.out.println(e.getMessage().toString());
}
}
Server handler:
#Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
Channel channel= ctx.getChannel();
channel.write(e.getMessage());
if(e.getMessage() instanceof String){
System.out.println((String)e.getMessage());
}
System.out.println(e.getMessage().toString());
}
client runner:
public static void main(String[] args) throws InterruptedException {
final int nMessages = 5;
try {
Client c = new Client();
if (!c.start()) {
return;
}
for (int i = 0; i < nMessages; i++) {
Thread.sleep(1L);
c.sendMessage((i + 1) + "\n");
}
c.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Server Runner:
public static void main(String[] args) {
final Server s = new Server("server1");
if (!s.start()) {
return;
}
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
s.stop();
}
});
}
now what I really need is to print the message that I wrote on the channel on both client and server side and I am really puzzled on this.
Your pipeline creation seems to be wrong at first look. At server side when decoding, the Delimiter needs to come first, then the StringDecoder and then the business handler. You could resolve this probably by just putting breakpoints in these decoders and encoders. Also take a look at this link for very good documentation on how this works.

Categories