|
@ -5,61 +5,68 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @created Airhead 2016/8/1.
|
|
* @created Airhead 2016/8/1.
|
|
*/
|
|
*/
|
|
public class BrokerServerClient {
|
|
public class BrokerServerClient {
|
|
|
|
private static Map<String, BrokerServer> mapBrokerServer = new HashMap<>();
|
|
private String host;
|
|
private String host;
|
|
|
|
|
|
public BrokerServerClient(String host) {
|
|
public BrokerServerClient(String host) {
|
|
this.host = host;
|
|
this.host = host;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public synchronized static void addBrokerServer(BrokerServer brokerServer) {
|
|
|
|
if (brokerServer.getEnable()) {
|
|
|
|
mapBrokerServer.put(brokerServer.getIdentity(), brokerServer);
|
|
|
|
} else {
|
|
|
|
mapBrokerServer.remove(brokerServer.getIdentity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public Response invokeSync(Request request) {
|
|
public Response invokeSync(Request request) {
|
|
|
|
Response response = new Response();
|
|
try {
|
|
try {
|
|
String brokerServer = getBrokerServer();
|
|
|
|
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
HttpPost httpPost = new HttpPost(brokerServer + "/gateway");
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
|
try {
|
|
|
|
response = httpclient.execute(httpPost);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
assert response != null;
|
|
|
|
response.close();
|
|
|
|
httpclient.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BrokerServer brokerServer = selectBrokerServer();
|
|
|
|
return brokerServer.invokeSync(request);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
response.setError(e);
|
|
|
|
response.setStackTrace(e.getStackTrace().toString());
|
|
}
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public Object invokeAsync() {
|
|
|
|
return null;
|
|
|
|
|
|
public void invokeAsync(Request request, final ResultCallback<Response> callback) {
|
|
|
|
BrokerServer brokerServer = selectBrokerServer();
|
|
|
|
brokerServer.invokeAsync(request, callback);
|
|
}
|
|
}
|
|
|
|
|
|
private String getBrokerServer() throws Exception {
|
|
|
|
|
|
private BrokerServer selectBrokerServer() {
|
|
|
|
if (mapBrokerServer.size() != 0) {
|
|
|
|
return mapBrokerServer.entrySet().iterator().next().getValue();
|
|
|
|
}
|
|
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
HttpGet httpGet = new HttpGet(host + ":10135/brokerServer");
|
|
HttpGet httpGet = new HttpGet(host + ":10135/brokerServer");
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
try {
|
|
try {
|
|
response = httpclient.execute(httpGet);
|
|
response = httpclient.execute(httpGet);
|
|
if (response.getStatusLine().getStatusCode() != 200) {
|
|
if (response.getStatusLine().getStatusCode() != 200) {
|
|
throw new Exception("Can not connect the server.");
|
|
|
|
|
|
throw new IOException("Can not connect the server.");
|
|
}
|
|
}
|
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
HttpEntity entity = response.getEntity();
|
|
String body = EntityUtils.toString(entity, "UTF-8");
|
|
String body = EntityUtils.toString(entity, "UTF-8");
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
@ -67,16 +74,24 @@ public class BrokerServerClient {
|
|
String hostAddress = node.path("hostAddress").asText();
|
|
String hostAddress = node.path("hostAddress").asText();
|
|
String port = node.path("port").asText();
|
|
String port = node.path("port").asText();
|
|
|
|
|
|
return "http://" + hostAddress + ":" + port;
|
|
|
|
|
|
|
|
|
|
BrokerServer brokerServer = new BrokerServer();
|
|
|
|
brokerServer.setIp(hostAddress);
|
|
|
|
brokerServer.setPort(port);
|
|
|
|
brokerServer.setEnable(true);
|
|
|
|
BrokerServerClient.addBrokerServer(brokerServer);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
throw new EsbException(e.getMessage(), e.getCause());
|
|
} finally {
|
|
} finally {
|
|
assert response != null;
|
|
assert response != null;
|
|
response.close();
|
|
|
|
httpclient.close();
|
|
|
|
|
|
try {
|
|
|
|
response.close();
|
|
|
|
httpclient.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
throw new Exception("Can not connect the server.");
|
|
|
|
|
|
return mapBrokerServer.entrySet().iterator().next().getValue();
|
|
}
|
|
}
|
|
}
|
|
}
|