DUBBO-354 增加异步示例

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1670 1a56cb94-b969-4eaa-88fa-be21384802f2
This commit is contained in:
william.liangf 2012-05-07 13:27:32 +00:00
parent c84adc6a9d
commit 7a3d6e61e9
6 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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.alibaba.dubbo.examples.async;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.examples.async.api.AsyncService;
import com.alibaba.dubbo.rpc.RpcContext;
/**
* CallbackConsumer
*
* @author william.liangf
*/
public class AsyncConsumer {
public static void main(String[] args) throws Exception {
String config = AsyncConsumer.class.getPackage().getName().replace('.', '/') + "/async-consumer.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
context.start();
final AsyncService asyncService = (AsyncService)context.getBean("cacheService");
Future<String> f = RpcContext.getContext().asyncCall(new Callable<String>() {
public String call() throws Exception {
return asyncService.sayHello("async call request");
}
});
System.out.println("async call ret :" + f.get());
RpcContext.getContext().asyncCall(new Runnable() {
public void run() {
asyncService.sayHello("oneway call request");
asyncService.sayHello("oneway call request");
}
});
System.in.read();
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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.alibaba.dubbo.examples.async;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* MergeProvider
*
* @author william.liangf
*/
public class AsyncProvider {
public static void main(String[] args) throws Exception {
String config = AsyncProvider.class.getPackage().getName().replace('.', '/') + "/callback-provider.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
context.start();
System.in.read();
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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.alibaba.dubbo.examples.async.api;
/**
* CallbackService
*
* @author william.liangf
*/
public interface AsyncService {
String sayHello(String name);
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="callback-consumer" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<dubbo:reference id="callbackService" interface="com.alibaba.dubbo.examples.callback.api.CallbackService" />
</beans>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="merge-provider" />
<dubbo:registry address="multicast://224.5.6.7:1234" />
<dubbo:protocol name="dubbo" port="20880" />
<bean id="callbackService" class="com.alibaba.dubbo.examples.callback.impl.CallbackServiceImpl" />
<dubbo:service interface="com.alibaba.dubbo.examples.callback.api.CallbackService" ref="callbackService" connections="1" callbacks="1000">
<dubbo:method name="addListener">
<dubbo:argument index="1" callback="true" />
<!--<dubbo:argument type="com.demo.CallbackListener" callback="true" />-->
</dubbo:method>
</dubbo:service>
</beans>

View File

@ -0,0 +1,31 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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.alibaba.dubbo.examples.async.impl;
import com.alibaba.dubbo.examples.async.api.AsyncService;
/**
* CallbackServiceImpl
*
* @author william.liangf
*/
public class AsyncServiceImpl implements AsyncService {
public String sayHello(String name) {
return "hello, " + name;
}
}