java venus,JAVA语言客户端

java语言例子:

b2ad95eec5ea880667efa809910a2b68.gif接口定义

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

接口定义

接口定义

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 接口例子.

* 支持3种调用方式:

*

请求应答模式:普通的request、response,一般用于接口有返回值

*

异步请求模式:通常用于接口无返回值,客户端并不关心服务器的处理结果,也不用关心服务器处理多少时间

*

异步回调模式:接口无返回值,处理通常消化大量时间,需要服务端通知处理结果的业务接口

*

* @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 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);

}

b2ad95eec5ea880667efa809910a2b68.gif客户端开发

1、引入venus相关依赖

2、编写Spring的配置文件

3、编写venus客户端配置文件:VenusClient-simple.xml

4、编写Testcase进行junit测试

1、依赖管理

maven pom.xml 增加依赖

com.meidusa.service

venus-client

${venus.version}

2.2.6

2、客户端的spring的配置

Spring xml配置内容

classpath:VenusClient-simple.xml

3、客户端的 venus.xml的配置

VenusClient-simple.xml 配置

127.0.0.1:16800

4、testCase using Spring autowire

使用Spring autowire功能

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");

}

}

b2ad95eec5ea880667efa809910a2b68.gif服务端开发

1、引入对venus的依赖

2、实现HelloService接口

3、Venus的服务端配置:VenusServices-simple.xml

4、Spring的相关Bean的配置(注意:需要让spring容器默认采用 byName autowire 的功能: default-autowire="byName")

1、引入Venus相关的依赖

maven pom.xml 增加依赖

com.meidusa.service

venus-backend

${venus.version}

2.2.6

2、DefaultHelloService的实现

HelloService默认实现

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 map = new HashMap();

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 invocationListener) {

Hello hello = new Hello();

hello.setName(name);

hello.setGreeting(greeting);

Map map = new HashMap();

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);

}

}

}

3、VenusService-simple.xml的配置

VenusServices-simple.xml 配置

hello venus hello service

4、Spring相关的venus 配置

Spring 配置相关的bean

classpath:VenusServices-simple.xml

b2ad95eec5ea880667efa809910a2b68.gif服务端的启动控制台展示

1、显示了多少个Service暴露出来

2、显示了每个Service有哪些Endpoint暴露出来

3、服务端的监听端口

服务器端运行展现

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.

helloworld源代码地址:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值