![JAR search and dependency download from the Maven repository](/logo.png)
org.frameworkset.spi.remote.http.HttpClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2008 biaoping.yin
*
* Licensed 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 org.frameworkset.spi.remote.http;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import org.apache.http.impl.nio.DefaultClientIOEventDispatch;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.ExceptionEvent;
import org.apache.http.nio.NHttpClientHandler;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.IOReactorExceptionHandler;
import org.apache.http.nio.reactor.IOReactorStatus;
import org.apache.http.nio.reactor.SessionRequest;
import org.apache.http.nio.reactor.SessionRequestCallback;
import org.apache.http.params.HttpParams;
/**
* Title: HttpClient.java
* Description:
* bboss workgroup
* Copyright (c) 2008
* @Date 2010-9-2
* @author biaoping.yin
* @version 1.0
*/
public class HttpClient {
private final DefaultConnectingIOReactor ioReactor;
private final HttpParams params;
private volatile IOReactorThread thread;
public HttpClient(final HttpParams params) throws IOException {
super();
this.ioReactor = new DefaultConnectingIOReactor(2, params);
this.params = params;
}
public HttpParams getParams() {
return this.params;
}
public void setExceptionHandler(final IOReactorExceptionHandler exceptionHandler) {
this.ioReactor.setExceptionHandler(exceptionHandler);
}
private void execute(final NHttpClientHandler clientHandler) throws IOException {
IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(
clientHandler,
this.params);
this.ioReactor.execute(ioEventDispatch);
}
static class InnerSessionRequestCallback implements SessionRequestCallback {
// private final CountDownLatch requestCount;
public InnerSessionRequestCallback() {
super();
// this.requestCount = requestCount;
}
public void cancelled(final SessionRequest request) {
System.out.println("Connect request cancelled: " + request.getRemoteAddress());
// this.requestCount.countDown();
}
public void completed(final SessionRequest request) {
}
public void failed(final SessionRequest request) {
System.out.println("Connect request failed: " + request.getRemoteAddress());
// this.requestCount.countDown();
}
public void timeout(final SessionRequest request) {
System.out.println("Connect request timed out: " + request.getRemoteAddress());
// this.requestCount.countDown();
}
}
public SessionRequest openConnection(final InetSocketAddress address, final Object attachment) {
return this.ioReactor.connect(address, null, attachment, new InnerSessionRequestCallback());
}
public void start(final NHttpClientHandler clientHandler) {
this.thread = new IOReactorThread(clientHandler);
this.thread.start();
}
public IOReactorStatus getStatus() {
return this.ioReactor.getStatus();
}
public List getAuditLog() {
return this.ioReactor.getAuditLog();
}
public void join(long timeout) throws InterruptedException {
if (this.thread != null) {
this.thread.join(timeout);
}
}
public Exception getException() {
if (this.thread != null) {
return this.thread.getException();
} else {
return null;
}
}
public void shutdown() throws IOException {
this.ioReactor.shutdown();
try {
join(500);
} catch (InterruptedException ignore) {
}
}
private class IOReactorThread extends Thread {
private final NHttpClientHandler clientHandler;
private volatile Exception ex;
public IOReactorThread(final NHttpClientHandler clientHandler) {
super();
this.clientHandler = clientHandler;
}
@Override
public void run() {
try {
execute(this.clientHandler);
} catch (Exception ex) {
this.ex = ex;
// if (requestCount != null) {
// requestCount.failure(ex);
// }
}
}
public Exception getException() {
return this.ex;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy