Ver código fonte

增加hos-client的java实现,还差最终的broker的http调用

Airhead 8 anos atrás
pai
commit
1fcc82a03f

+ 3 - 0
sdk/java/hos-client/ReadMe.md

@ -0,0 +1,3 @@
#TODO
1. 具体的请求实现
1. Linstener动态更新Broker Server缓存

+ 7 - 0
sdk/java/hos-client/hos-client.iml

@ -9,5 +9,12 @@
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.1" level="project" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.3" level="project" />
    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.6.4" level="project" />
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.6.0" level="project" />
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.6.4" level="project" />
  </component>
</module>

+ 10 - 5
sdk/java/hos-client/pom.xml

@ -8,11 +8,16 @@
    <artifactId>hos-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>com.yihu.hos.resource</groupId>-->
            <!--<artifactId>Hos-Framework</artifactId>-->
            <!--<version>1.0.7</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.4</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

+ 55 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/BrokerServer.java

@ -0,0 +1,55 @@
package com.yihu.hos.client;
/**
 * @created Airhead 2016/8/16.
 */
public class BrokerServer {
    private String host;
    private String ip;
    private String port;
    private Boolean enable;
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public Boolean getEnable() {
        return enable;
    }
    public void setEnable(Boolean enable) {
        this.enable = enable;
    }
    public String getIdentity() {
        return this.ip + ":" + this.port;
    }
    public Response invokeSync(Request request) {
        return null;
    }
    public void invokeAsync(Request request, final ResultCallback<Response> callback) {
    }
}

+ 41 - 26
sdk/java/hos-client/src/main/java/com/yihu/hos/client/BrokerServerClient.java

@ -5,61 +5,68 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * @created Airhead 2016/8/1.
 */
public class BrokerServerClient {
    private static Map<String, BrokerServer> mapBrokerServer = new HashMap<>();
    private String host;
    public BrokerServerClient(String 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) {
        Response response = new Response();
        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) {
            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();
        HttpGet httpGet = new HttpGet(host + ":10135/brokerServer");
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
            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();
            String body = EntityUtils.toString(entity, "UTF-8");
            ObjectMapper objectMapper = new ObjectMapper();
@ -67,16 +74,24 @@ public class BrokerServerClient {
            String hostAddress = node.path("hostAddress").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) {
            e.printStackTrace();
            throw new EsbException(e.getMessage(), e.getCause());
        } finally {
            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();
    }
}

+ 44 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/EsbException.java

@ -0,0 +1,44 @@
/**
 * The MIT License (MIT)
 * Copyright (c) 2009-2015 HONG LEIMING
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.yihu.hos.client;
public class EsbException extends RuntimeException {
	private static final long serialVersionUID = 8445590420018236422L;
	public EsbException(String message) {
		super(message); 
	}
	public EsbException() {
	}
	public EsbException(String message, Throwable cause) {
		super(message, cause); 
	}
	public EsbException(Throwable cause) {
		super(cause); 
	}
}

+ 29 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/Request.java

@ -1,7 +1,36 @@
package com.yihu.hos.client;
import java.util.HashMap;
import java.util.Map;
/**
 * @created Airhead 2016/8/5.
 */
public class Request {
    private String method;
    private Map<String, String> args;
    public String getMethod() {
        return method;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    public Map<String, String> getArgs() {
        return args;
    }
    public void setArgs(Map<String, String> args) {
        this.args = args;
    }
    public void addArg(String name, String value) {
        if (args == null) {
            args = new HashMap<>();
        }
        args.put(name, value);
    }
}

+ 51 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/Response.java

@ -1,7 +1,58 @@
package com.yihu.hos.client;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
 * @created Airhead 2016/8/5.
 */
public class Response {
    public static final String KEY_RESULT = "result";
    public static final String KEY_STACK_TRACE = "stackTrace";
    public static final String KEY_ERROR = "error";
    private Object result;
    private Throwable error;
    private String stackTrace; //异常时候一定保证stackTrace设定,判断的逻辑以此为依据
    private String encoding = "UTF-8";
    public Object getResult() {
        return result;
    }
    public void setResult(Object result) {
        this.result = result;
    }
    public Throwable getError() {
        return error;
    }
    public void setError(Throwable error) {
        this.error = error;
        if (error == null) return;
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        if (error.getCause() != null) {
            error = error.getCause();
        }
        error.printStackTrace(pw);
        this.stackTrace = sw.toString();
    }
    public String getStackTrace() {
        return stackTrace;
    }
    public void setStackTrace(String stackTrace) {
        this.stackTrace = stackTrace;
    }
    public String getEncoding() {
        return encoding;
    }
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
}

+ 11 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/ResultCallback.java

@ -0,0 +1,11 @@
package com.yihu.hos.client;
/**
 * Asynchronous message callback
 * @author rushmore (洪磊明)
 *
 * @param <T> returned message type
 */
public interface ResultCallback<T> {
    public void onReturn(T result);
}