This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git
The following commit(s) were added to refs/heads/master by this push: new 70f556e embedded call in async provider (#419) 70f556e is described below commit 70f556eac935720b529cdf5418a0932ef66773fe Author: ken.lj <ken.lj...@gmail.com> AuthorDate: Thu Jan 27 14:45:07 2022 +0800 embedded call in async provider (#419) --- .../case-configuration.yml | 67 ++++++++++++++++++++++ .../dubbo/samples/async/EmbeddedAsyncProvider.java | 38 ++++++++++++ .../samples/async/api/EmbeddedAsyncService.java | 29 ++++++++++ .../dubbo/samples/async/impl/AsyncServiceImpl.java | 15 ++++- .../async/impl/EmbeddedAsyncServiceImpl.java} | 27 ++++----- .../resources/META-INF/spring/async-provider.xml | 9 ++- ...nc-provider.xml => embedded-async-provider.xml} | 10 ++-- .../apache/dubo/sample/async/AsyncServiceIT.java | 2 +- 8 files changed, 170 insertions(+), 27 deletions(-) diff --git a/dubbo-samples-async/dubbo-samples-async-provider/case-configuration.yml b/dubbo-samples-async/dubbo-samples-async-provider/case-configuration.yml index df96773..c1bf675 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/case-configuration.yml +++ b/dubbo-samples-async/dubbo-samples-async-provider/case-configuration.yml @@ -22,3 +22,70 @@ props: zookeeper_port: 2181 dubbo_port: 20880 + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + + +services: + zookeeper: + image: zookeeper:latest + + async-provider: + type: app + basedir: dubbo-samples-async-provider + mainClass: org.apache.dubbo.samples.async.AsyncProvider + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20880 + checkLog: "dubbo service started" + depends_on: + - zookeeper + + async-provider-embedded: + type: app + basedir: dubbo-samples-async-provider + mainClass: org.apache.dubbo.samples.async.EmbeddedAsyncProvider + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 20881 + checkLog: "dubbo service started" + depends_on: + - zookeeper + + async-consumer-test: + type: test + basedir: servicediscovery-consumer-old + tests: + - "**/*IT.class" + systemProps: + - zookeeper.address=zookeeper + waitPortsBeforeRun: + - zookeeper:2181 + - provider-embedded:20880 + - async-provider-embedded:20881 + depends_on: + - zookeeper + - async-provider + - async-provider-embedded + + diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/EmbeddedAsyncProvider.java b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/EmbeddedAsyncProvider.java new file mode 100644 index 0000000..e34459c --- /dev/null +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/EmbeddedAsyncProvider.java @@ -0,0 +1,38 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.samples.async; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.concurrent.CountDownLatch; + +public class EmbeddedAsyncProvider { + + public static void main(String[] args) throws Exception { + new EmbeddedZooKeeper(2181, false).start(); + + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/embedded-async-provider.xml"); + context.start(); + + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + +} diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/api/EmbeddedAsyncService.java b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/api/EmbeddedAsyncService.java new file mode 100644 index 0000000..fb69c63 --- /dev/null +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/api/EmbeddedAsyncService.java @@ -0,0 +1,29 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.samples.async.api; + +/** + * Dubbo service called by AsyncServiceImpl + */ +public interface EmbeddedAsyncService { + + String sayHello(String name); + +} diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/AsyncServiceImpl.java b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/AsyncServiceImpl.java index de635d4..c3fb98f 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/AsyncServiceImpl.java +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/AsyncServiceImpl.java @@ -22,6 +22,7 @@ package org.apache.dubbo.samples.async.impl; import org.apache.dubbo.rpc.AsyncContext; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.samples.async.api.AsyncService; +import org.apache.dubbo.samples.async.api.EmbeddedAsyncService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +30,8 @@ import org.slf4j.LoggerFactory; public class AsyncServiceImpl implements AsyncService { private static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class); + private EmbeddedAsyncService embeddedService; + @Override public String sayHello(String name) { AsyncContext asyncContext = RpcContext.startAsync(); @@ -38,12 +41,15 @@ public class AsyncServiceImpl implements AsyncService { asyncContext.signalContextSwitch(); logger.info("Attachment from consumer: " + RpcContext.getContext().getAttachment("consumer-key1")); logger.info("async start"); + String embeddedCallResult = null; try { + embeddedCallResult = embeddedService.sayHello("embedded call"); + logger.info(" embedded call result is " + embeddedCallResult); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } - asyncContext.write("Hello " + name + ", response from provider."); + asyncContext.write("Hello " + name + ", " + embeddedCallResult + " response from provider."); logger.info("async end"); }).start(); @@ -51,4 +57,11 @@ public class AsyncServiceImpl implements AsyncService { return "hello, " + name; } + public EmbeddedAsyncService getEmbeddedService() { + return embeddedService; + } + + public void setEmbeddedService(EmbeddedAsyncService embeddedService) { + this.embeddedService = embeddedService; + } } diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/EmbeddedAsyncServiceImpl.java similarity index 52% copy from dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java copy to dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/EmbeddedAsyncServiceImpl.java index 84d42ac..aa8a71a 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/java/org/apache/dubbo/samples/async/impl/EmbeddedAsyncServiceImpl.java @@ -14,26 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.dubbo.samples.async.impl; -package org.apache.dubo.sample.async; +import org.apache.dubbo.samples.async.api.EmbeddedAsyncService; -import org.apache.dubbo.samples.async.api.AsyncService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import org.junit.Assert; -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; +public class EmbeddedAsyncServiceImpl implements EmbeddedAsyncService { + private static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class); -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = {"classpath:/META-INF/spring/async-consumer.xml"}) -public class AsyncServiceIT { - @Autowired - private AsyncService asyncService; - - @Test - public void test() throws Exception { - Assert.assertEquals("Hello dubbo, response from provider.", asyncService.sayHello("dubbo")); + @Override + public String sayHello(String name) { + logger.info("Received embedded call from async service impl."); + return "embedded service"; } } diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml b/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml index e379d91..4480b31 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml @@ -33,8 +33,13 @@ <dubbo:provider token="true"/> - <bean id="asyncService" class="org.apache.dubbo.samples.async.impl.AsyncServiceImpl"/> - + <bean id="asyncService" class="org.apache.dubbo.samples.async.impl.AsyncServiceImpl"> + <property name="embeddedService" ref="embeddedAsyncService"/> + </bean> <dubbo:service interface="org.apache.dubbo.samples.async.api.AsyncService" ref="asyncService"/> + <dubbo:reference interface="org.apache.dubbo.samples.async.api.EmbeddedAsyncService" id="embeddedAsyncService" + check="false"/> + + </beans> diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml b/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/embedded-async-provider.xml similarity index 86% copy from dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml copy to dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/embedded-async-provider.xml index e379d91..ebe8d94 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/async-provider.xml +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/main/resources/META-INF/spring/embedded-async-provider.xml @@ -25,16 +25,14 @@ http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder/> - <dubbo:application name="async-provider"/> + <dubbo:application name="embedded-async-provider"/> <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> - <dubbo:protocol name="dubbo" port="20880"/> + <dubbo:protocol name="dubbo" port="20881"/> - <dubbo:provider token="true"/> + <bean id="embeddedAsyncService" class="org.apache.dubbo.samples.async.impl.EmbeddedAsyncServiceImpl"/> - <bean id="asyncService" class="org.apache.dubbo.samples.async.impl.AsyncServiceImpl"/> - - <dubbo:service interface="org.apache.dubbo.samples.async.api.AsyncService" ref="asyncService"/> + <dubbo:service interface="org.apache.dubbo.samples.async.api.EmbeddedAsyncService" ref="embeddedAsyncService"/> </beans> diff --git a/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java b/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java index 84d42ac..a42ec53 100644 --- a/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java +++ b/dubbo-samples-async/dubbo-samples-async-provider/src/test/java/org/apache/dubo/sample/async/AsyncServiceIT.java @@ -34,6 +34,6 @@ public class AsyncServiceIT { @Test public void test() throws Exception { - Assert.assertEquals("Hello dubbo, response from provider.", asyncService.sayHello("dubbo")); + Assert.assertEquals("Hello dubbo, embedded service response from provider.", asyncService.sayHello("dubbo")); } } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org