View Source

h2. java语言例子:

{tip:title=接口定义}接口:用来约束客户端、服务端的应用层协议,他申明了具体服务接口的调用方式,参数的数据类型,具体功能的声明、异常的描述等等信息,它有服务端开发工程师,服务使用方工程师共同制定的。{tip}
* 接口定义 


{code:language=java|title=接口定义|linenumbers=true|firstline=0}
package com.meidusa.venus.hello.api;

import com.meidusa.venus.annotations.Endpoint;
import com.meidusa.venus.annotations.Param;
import com.meidusa.venus.annotations.Service;
import com.meidusa.venus.notify.InvocationListener;

/**
* Service framework的 HelloService 接口例子.</p>
* 支持3种调用方式:</p>
* <li> 请求应答模式:普通的request、response,一般用于接口有返回值</li>
* <li> 异步请求模式:通常用于接口无返回值,客户端并不关心服务器的处理结果,也不用关心服务器处理多少时间</li>
* <li> 异步回调模式:接口无返回值,处理通常消化大量时间,需要服务端通知处理结果的业务接口</li>
*
* @author Struct
*
*/
@Service(name="HelloService",version=1)
public interface HelloService {

/**
* 无返回结果的服务调用,支持回调方式,该服务在通讯层面上为异步调用
* @param name
* @param invocationListener 客户端的回调接口
*/
@Endpoint(name="sayHelloWithCallbak")
public abstract void sayHello(@Param(name="name") String name,
@Param(name="callback") InvocationListener<Hello> invocationListener);
/**
* 无返回结果的服务调用,支持同步或者异步调用,
* 该接口申明:同步,并且接口申明异常
* @param name
*/
@Endpoint(name="sayHello",async=false)
public abstract void sayHello(@Param(name="name") String name) throws HelloNotFoundException;

/**
* 无返回结果的服务调用,支持同步或者异步调用,无异常申明
* @param name
*/
@Endpoint(name="sayAsyncHello",async=true)
public abstract void sayAsyncHello(@Param(name="name") String name);


/**
* 有返回结果的服务调用,该接口只能支持同步调用
* @param name
* @return
*/
@Endpoint(name="getHello")
public abstract Hello getHello(@Param(name="name") String name);
}

{code}


{tip:title=客户端开发}
1、引入venus相关依赖
2、编写Spring的配置文件
3、编写venus客户端配置文件:VenusClient-simple.xml
4、编写Testcase进行junit测试
{tip}

1、依赖管理

{code:language=xml|title=maven pom.xml 增加依赖|linenumbers=true|firstline=1|theme=Eclipse}
<dependencies>
<dependency>
<groupId>com.meidusa.venus</groupId>
<artifactId>venus-client</artifactId>
<version>${venus.version}</version>
</dependency>
</dependencies>
<properties>
<venus.version>3.2.19</venus.version>
</properties>
{code}

2、客户端的spring的配置

{code:language=xml|title=Spring xml配置内容 |linenumbers=true|firstline=1|theme=Eclipse}
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="serviceFactory" class="com.meidusa.venus.client.VenusServiceFactory">
<property name="configFiles">
<list>
<value>classpath:VenusClient-simple.xml</value>
<!--
支持file: 绝对路径与classpath:,这儿可以配置多个文件
<value>file:VenusClient-simple.xml</value>
-->
</list>
</property>
</bean>
{code}
3、客户端的 venus.xml的配置
{code:language=xml|title=VenusClient-simple.xml 配置|linenumbers=true|theme=Eclipse}
<?xml version="1.0" encoding="utf8"?>
<venus-client>

<!-- 服务接口列表 -->
<services>
<!-- 定义服务以及指定该服务提供方的远程相关配置 -->
<service type="com.meidusa.venus.hello.api.HelloService">
<property name="ipAddressList">127.0.0.1:16800</property>
</service>
</services>
</venus-client>

{code}

高级使用指南,请参考 地址:&nbsp;[Venus:2. 高级使用指南]&nbsp;这儿涉及到 {参数校验、连接池、高可用,服务器端拦截器}

4、testCase using Spring autowire

{code:language=java|title=使用Spring autowire功能|linenumbers=true|firstline=1|theme=Eclipse}
import org.springframework.beans.factory.annotation.Autowired;

package com.meidusa.venus.hello.client;

import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.meidusa.venus.exception.CodedException;
import com.meidusa.venus.hello.api.Hello;
import com.meidusa.venus.hello.api.HelloNotFoundException;
import com.meidusa.venus.hello.api.HelloService;
import com.meidusa.venus.notify.InvocationListener;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/applicationContext-helloworld-client.xml")
public class TestHelloService {

@Autowired
private HelloService helloService;

@Test
public void saySync(){
System.out.println(helloService.getHello("jack"));
}

@Test
public void testSyncWithException(){
try {
helloService.sayHello("jack");
} catch (HelloNotFoundException e) {
System.out.println("throw an user defined HelloNotFoundException");
}
}

@Test
public void testAsync(){
helloService.sayAsyncHello("jack");
}

}

{code}



{tip:title=服务端开发}
1、引入对venus的依赖
2、实现HelloService接口
3、Venus的服务端配置:VenusServices-simple.xml
4、Spring的相关Bean的配置(注意:需要让spring容器默认采用 byName autowire 的功能:&nbsp;default-autowire="byName")
{tip}
1、引入Venus相关的依赖&nbsp;

{code:language=xml|title=maven pom.xml 增加依赖|linenumbers=true|firstline=1|theme=Eclipse}
<dependencies>
<dependency>
<groupId>com.meidusa.venus</groupId>
<artifactId>venus-backend</artifactId>
<version>${venus.version}</version>
</dependency>

</dependencies>
<properties>
<venus.version>3.2.19</venus.version>
</properties>


{code}

2、DefaultHelloService的实现

{code:language=java|title=HelloService默认实现|linenumbers=true|firstline=1|theme=Eclipse}
package com.meidusa.venus.hello.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.meidusa.venus.hello.api.Hello;
import com.meidusa.venus.hello.api.HelloNotFoundException;
import com.meidusa.venus.hello.api.HelloService;
import com.meidusa.venus.notify.InvocationListener;

public class DefaultHelloService implements HelloService {
private String greeting;
public String getGreeting() {
return greeting;
}

public void setGreeting(String greeting) {
this.greeting = greeting;
}
public Hello getHello(String name) {
Hello hello = new Hello();
hello.setName(name);
hello.setGreeting(greeting);
Map<String,Object> map = new HashMap<String,Object>();
hello.setMap(map);
map.put("1", 1);
map.put("2", new Long(2));
map.put("3", new Integer(3));
hello.setBigDecimal(new BigDecimal("1.341241233412"));
return hello;
}

public void sayHello(String name) throws HelloNotFoundException {
throw new HelloNotFoundException(name +" not found");
}

@Override
public void sayAsyncHello(String name) {
System.out.println("method sayAsyncHello invoked");
}

public void sayHello(String name,
InvocationListener<Hello> invocationListener) {
Hello hello = new Hello();
hello.setName(name);
hello.setGreeting(greeting);
Map<String,Object> map = new HashMap<String,Object>();
hello.setMap(map);
map.put("1", 1);
map.put("2", new Long(2));
map.put("3", new Integer(3));

if(invocationListener != null){
invocationListener.callback(hello);
}

}
}

{code}

3、VenusService-simple.xml的配置
{code:language=xml|title=VenusServices-simple.xml 配置|linenumbers=true|firstline=1|theme=Eclipse}
<?xml version="1.0" encoding="utf-8"?>
<venus-server>
<services>
<!-- 有2种方式暴露Service,1: instance由venus负责实例化,如下代码: -->
<service type="com.meidusa.venus.hello.api.HelloService">
<instance class="com.meidusa.venus.hello.impl.DefaultHelloService">
<property name="greeting">hello venus hello service</property>
</instance>
</service>


<!-- 第二种:instance由Spring负责提供,则需要采用${...} 这种方式引用Spring的bean,这种方式主要是Spring内其他bean依赖该bean,则通常采用这种 -->
<!-- 这儿的 ${helloService} 表示在你的spring xml中需要配置一个 beanName为 helloService,type为DefaultHelloService的一个bean
<service type="com.meidusa.venus.hello.api.HelloService">
<property name="instance">${helloService}</property>
</service> -->
</services>
</venus-server>

{code}

4、Spring相关的venus 配置
{code:language=xml|title=Spring 配置相关的bean|linenumbers=true|firstline=1|theme=Eclipse}
<!-- 引入venus Service 相关的配置 -->
<import resource="classpath:/spring/applicationContext-service-container.xml"/>

<!-- 加载服务配置文件 -->
<bean id="serviceManager" class="com.meidusa.venus.backend.services.xml.XmlFileServiceManager">
<property name="configFiles">
<list>
<value>classpath:VenusServices-simple.xml</value>
</list>
</property>
</bean>

<!-- 设置authenticateProvider,默认为可以使用该配置 -->
<!-- venus目前提供2种方式的认证,一种是默认的采用虚拟的方式,另外一种支持用户名/密码登录,你也可以自己实现provider -->
<bean id="authenticateProvider" class="com.meidusa.venus.backend.authenticate.SimpleAuthenticateProvider">
<property name="useDummy" value="true"/>
<property name="username" value="venus"/>
<property name="password" value="venus"/>
</bean>
{code}

{tip:title=服务端的启动控制台展示}
1、显示了多少个Service暴露出来
2、显示了每个Service有哪些Endpoint暴露出来
3、服务端的监听端口
{tip}

{code:language=bash|title=服务器端运行展现|linenumbers=true|firstline=1|theme=Eclipse}
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.sayHello
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.sayHelloWithCallbak
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.sayAsyncHello
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.getHello
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: ParameterizedService.arrayLong
2011-11-28 20:52:01,242 INFO xml.XmlFileServiceManager - Add Endpoint: ParameterizedService.arraylong
2011-11-28 20:52:01,258 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:16800.
{code}



h2. PropertyPlaceholder:用于替换venus的{color:#ff0000}配置文件{color}中包含 ${}变量的东西
{code:language=xml|title=PropertyPlaceholder|linenumbers=true|firstline=1|theme=Eclipse}
<bean class="com.meidusa.venus.spring.VenusPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${project.home:.}/application.properties</value>

</list>
</property>
</bean>
{code}


h2. helloworld源代码地址:

svn 地址:svn://svn.hexnova.com/venus/venus-helloworld/trunk

svn的用户名:guest

svn密码:guest