Skip to content

Commit

Permalink
客户端发布API增加对 jprotobuf [https://github.com/jhunters/JProtobuf] 的@protobuf
Browse files Browse the repository at this point in the history
注解 的POJO类的支持
  • Loading branch information
xiemalin committed Oct 10, 2014
1 parent eebe054 commit 3f15c00
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.class
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,61 @@ RPC客户端使用IDLProxyFactoryBean进行访问,示例代码如下:

}
```
### RPC客户端Spring配置 ###
RPC客户端使用AnnotationProxyFactoryBean进行访问,示例代码如下:
```java
@Test
public void testClientProxy() throws Exception {
AnnotationProxyFactoryBean<StringMessagePOJO, StringMessagePOJO> factoryBean;

factoryBean = new AnnotationProxyFactoryBean<StringMessagePOJO, StringMessagePOJO>();
factoryBean.setServiceUrl("http://localhost:8080/myfirstproject/remoting/SimpleIDLTest");

factoryBean.setInputClass(StringMessagePOJO.class);
factoryBean.setOutputClass(StringMessagePOJO.class);

factoryBean.afterPropertiesSet();

ClientInvoker<StringMessagePOJO, StringMessagePOJO> invoker = factoryBean.getObject();

StringMessagePOJO input = invoker.getInput();
if (input != null) {
input.setList("how are you!");
}

StringMessagePOJO output = invoker.invoke(input);
if (output != null) {
System.out.println(output.getList());
}
}
```
StringMessagePOJO对象代码:
```java
public class StringMessagePOJO {

@Protobuf(fieldType = FieldType.STRING, order = 1, required = true)
private String list;

/**
* get the list
* @return the list
*/
public String getList() {
return list;
}

/**
* set list value to list
* @param list the list to set
*/
public void setList(String list) {
this.list = list;
}


}
```
### RPC客户端Spring配置 ###
RPC客户端使用IDLProxyFactoryBean进行访问
```xml
<bean id="simpleTestClientForIDLProxy" class="com.baidu.jprotobuf.rpc.client.IDLProxyFactoryBean">
<property name="inputIDL" value="classpath:/simplestring.proto"></property>
Expand All @@ -134,6 +187,14 @@ RPC客户端使用IDLProxyFactoryBean进行访问,示例代码如下:
<property name="serviceUrl" value="http://localhost:8080/myfirstproject/remoting/SimpleIDLTest"></property>
</bean>
```
RPC客户端使用AnnotationProxyFactoryBean进行访问
```xml
<bean id="simpleTestClientForAnnotationProxy" class="com.baidu.jprotobuf.rpc.client.AnnotationProxyFactoryBean">
<property name="inputClass" value="com.baidu.bjf.remoting.protobuf.FieldType.StringMessagePOJO"></property>
<property name="outputClass" value="com.baidu.bjf.remoting.protobuf.FieldType.StringMessagePOJO"></property>
<property name="serviceUrl" value="http://localhost:8080/myfirstproject/remoting/SimpleIDLTest"></property>
</bean>
```

### 多个IDL message定义解决方案 ###
例如下面定义了多个message定义时,则在服务发布以及客户连接时,需要指定objectName
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<property name="classes.dir" value="build" />
<property name="lib.dir" value="lib" />

<property name="jar.file" value="jprotobuf-rpc-http-1.0.0.jar" />
<property name="jar.file" value="jprotobuf-rpc-http-1.1.0.jar" />

<property name="encoding" value="UTF-8" />

Expand Down
Binary file renamed lib/jprotobuf-1.0.4.jar → lib/jprotobuf-1.0.5.jar
Binary file not shown.
Binary file added output/jprotobuf-rpc-http-1.1.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* 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 com.baidu.jprotobuf.rpc.client;

/**
*
* @author xiemalin
* @since 1.1.0
*/
public abstract class AbstractProxyFactoryBean {

/**
* service url
*/
private String serviceUrl;

/**
* connection time out
*/
private int connectTimeout = -1;

/**
* read time out
*/
private int readTimeout = -1;

/**
* set serviceUrl value to serviceUrl
* @param serviceUrl the serviceUrl to set
*/
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}

/**
* set connectTimeout value to connectTimeout
* @param connectTimeout the connectTimeout to set
*/
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}

/**
* set readTimeout value to readTimeout
* @param readTimeout the readTimeout to set
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}

/**
* get the serviceUrl
* @return the serviceUrl
*/
protected String getServiceUrl() {
return serviceUrl;
}

/**
* get the connectTimeout
* @return the connectTimeout
*/
protected int getConnectTimeout() {
return connectTimeout;
}

/**
* get the readTimeout
* @return the readTimeout
*/
protected int getReadTimeout() {
return readTimeout;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* 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 com.baidu.jprotobuf.rpc.client;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.IDLProxyObject;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;

/**
*
* @author xiemalin
* @since 1.1.0
*/
public class AnnotationProxyFactoryBean<I, O> extends AbstractProxyFactoryBean implements
FactoryBean<ClientInvoker<I, O>>, InitializingBean {

private IDLHttpClientInvoker invoker;

private Codec<? extends Object> inputCodec;
private Codec<? extends Object> outputCodec;

ClientInvoker<I, O> proxy = new ClientInvoker<I, O>() {

@Override
public I getInput() {
if (inputClass == null) {
return null;
}
try {
return (I) inputClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public O invoke(I input) throws Exception {
IDLProxyObject inputObject = null;
if (inputClass != null) {
inputObject = new IDLProxyObject(inputCodec, input, inputClass);
}

IDLProxyObject result = invoker.invoke(inputObject);
if (result == null) {
return null;
}
return (O) result.getTarget();
}

};

private Class<I> inputClass;
private Class<O> outputClass;

/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
@Override
public ClientInvoker<I, O> getObject() throws Exception {
return proxy;
}

/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
@Override
public Class<?> getObjectType() {
return IDLHttpClientInvoker.class;
}

/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {
return true;
}

/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
IDLProxyObject inputIDLProxyObject = null;
if (inputClass != null) {
inputCodec = ProtobufProxy.create(inputClass);
I input = inputClass.newInstance();
inputIDLProxyObject = new IDLProxyObject(inputCodec, input, inputClass);
}
IDLProxyObject outputIDLProxyObject = null;
if (outputClass != null) {
outputCodec = ProtobufProxy.create(outputClass);
O output = outputClass.newInstance();
outputIDLProxyObject = new IDLProxyObject(outputCodec, output, outputClass);
}

invoker = new IDLHttpClientInvoker(getServiceUrl(), inputIDLProxyObject, outputIDLProxyObject);
invoker.setConnectTimeout(getConnectTimeout());
invoker.setReadTimeout(getReadTimeout());

}

/**
* set inputClass value to inputClass
* @param inputClass the inputClass to set
*/
public void setInputClass(Class<I> inputClass) {
this.inputClass = inputClass;
}

/**
* set outputClass value to outputClass
* @param outputClass the outputClass to set
*/
public void setOutputClass(Class<O> outputClass) {
this.outputClass = outputClass;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.baidu.jprotobuf.rpc.client;

import com.baidu.bjf.remoting.protobuf.IDLProxyObject;

/**
*
Expand All @@ -24,15 +23,15 @@
* @author xiemalin
* @since 1.0.0
*/
public interface ClientInvoker {
public interface ClientInvoker<I, O> {

IDLProxyObject getInput();
I getInput();

/**
* do RPC invoke action.
*
* @param param
* @return
*/
IDLProxyObject invoke(IDLProxyObject input) throws Exception;
O invoke(I input) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author xiemalin
* @since 1.0.0
*/
public class IDLHttpClientInvoker implements ClientInvoker {
public class IDLHttpClientInvoker implements ClientInvoker<IDLProxyObject, IDLProxyObject> {

private IDLProxyObject input;
private IDLProxyObject output;
Expand Down
Loading

0 comments on commit 3f15c00

Please sign in to comment.