This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git
commit e8c2ef475e18b3a82db738c58cf2554c27959bba Author: Eric Lee <[email protected]> AuthorDate: Sun Dec 24 00:19:06 2017 +0800 updated package path Signed-off-by: Eric Lee <[email protected]> --- .../omega-transport-resttemplate/pom.xml | 48 +++++++++++++++++ .../transport/resttemplate/RestTemplateConfig.java | 46 +++++++++++++++++ .../TransactionClientHttpRequestInterceptor.java | 49 ++++++++++++++++++ .../transport/resttemplate/UniqueIdGenerator.java | 30 +++++++++++ ...ransactionClientHttpRequestInterceptorTest.java | 25 +++++++-- .../resttemplate/UniqueIdGeneratorTest.java | 60 ++++++++++++++++++++++ omega/omega-transport/pom.xml | 34 +++++++----- .../saga/omega/transport/RestTemplateConfig.java | 20 -------- .../TransactionClientHttpRequestInterceptor.java | 27 ---------- .../saga/omega/transport/UniqueIdGenerator.java | 12 ----- .../omega/transport/UniqueIdGeneratorTest.java | 24 --------- 11 files changed, 277 insertions(+), 98 deletions(-) diff --git a/omega/omega-transport/omega-transport-resttemplate/pom.xml b/omega/omega-transport/omega-transport-resttemplate/pom.xml new file mode 100644 index 0000000..d7115d1 --- /dev/null +++ b/omega/omega-transport/omega-transport-resttemplate/pom.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + ~ + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>omega-transport</artifactId> + <groupId>io.servicecomb.saga</groupId> + <version>0.0.3-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>omega-transport-resttemplate</artifactId> + + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + </dependency> + </dependencies> + +</project> \ No newline at end of file diff --git a/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/RestTemplateConfig.java b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/RestTemplateConfig.java new file mode 100644 index 0000000..3a14e4b --- /dev/null +++ b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/RestTemplateConfig.java @@ -0,0 +1,46 @@ +/* + * 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 io.servicecomb.saga.omega.transport.resttemplate; + +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.web.client.RestTemplate; + +import io.servicecomb.saga.core.IdGenerator; + +@Configuration +public class RestTemplateConfig { + + @Bean + IdGenerator<String> idGenerator() { + return new UniqueIdGenerator(); + } + + @Bean + public RestTemplate restTemplate(IdGenerator<String> idGenerator) { + RestTemplate template = new RestTemplate(); + List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors(); + interceptors.add(new TransactionClientHttpRequestInterceptor(idGenerator)); + template.setInterceptors(interceptors); + return template; + } +} diff --git a/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java new file mode 100644 index 0000000..f913489 --- /dev/null +++ b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java @@ -0,0 +1,49 @@ +/* + * 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 io.servicecomb.saga.omega.transport.resttemplate; + +import java.io.IOException; + +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; + +import io.servicecomb.saga.core.IdGenerator; + +public class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { + + public static final String TRANSACTION_ID_KEY = "X-Transaction-Id"; + + private final IdGenerator<String> idGenerator; + + public TransactionClientHttpRequestInterceptor(IdGenerator<String> idGenerator) { + this.idGenerator = idGenerator; + } + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, + ClientHttpRequestExecution execution) throws IOException { + if (!request.getHeaders().containsKey(TRANSACTION_ID_KEY)) { + String txId = idGenerator.nextId(); + request.getHeaders().add(TRANSACTION_ID_KEY, txId); + } + return execution.execute(request, body); + } +} diff --git a/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGenerator.java b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGenerator.java new file mode 100644 index 0000000..17ef7ce --- /dev/null +++ b/omega/omega-transport/omega-transport-resttemplate/src/main/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGenerator.java @@ -0,0 +1,30 @@ +/* + * 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 io.servicecomb.saga.omega.transport.resttemplate; + +import java.util.UUID; + +import io.servicecomb.saga.core.IdGenerator; + +public class UniqueIdGenerator implements IdGenerator<String> { + @Override + public String nextId() { + return UUID.randomUUID().toString(); + } +} diff --git a/omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptorTest.java b/omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java similarity index 64% rename from omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptorTest.java rename to omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java index e0a180d..fa79858 100644 --- a/omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptorTest.java +++ b/omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java @@ -1,6 +1,24 @@ -package io.servicecomb.saga.omega.transport; +/* + * 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. + * + */ -import static io.servicecomb.saga.omega.transport.TransactionClientHttpRequestInterceptor.TRANSACTION_ID_KEY; +package io.servicecomb.saga.omega.transport.resttemplate; + +import static io.servicecomb.saga.omega.transport.resttemplate.TransactionClientHttpRequestInterceptor.TRANSACTION_ID_KEY; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; @@ -29,7 +47,8 @@ public class TransactionClientHttpRequestInterceptorTest { private ClientHttpResponse response = mock(ClientHttpResponse.class); - private ClientHttpRequestInterceptor clientHttpRequestInterceptor = new TransactionClientHttpRequestInterceptor(); + private ClientHttpRequestInterceptor clientHttpRequestInterceptor = new TransactionClientHttpRequestInterceptor( + new UniqueIdGenerator()); @Test public void newTransactionIdInHeaderIfNonExists() throws IOException { diff --git a/omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java b/omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java new file mode 100644 index 0000000..87338ea --- /dev/null +++ b/omega/omega-transport/omega-transport-resttemplate/src/test/java/io/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java @@ -0,0 +1,60 @@ +/* + * 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 io.servicecomb.saga.omega.transport.resttemplate; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.Test; + +public class UniqueIdGeneratorTest { + + private final UniqueIdGenerator idGenerator = new UniqueIdGenerator(); + + private Callable<String> task = idGenerator::nextId; + + @Test + public void nextIdIsUnique() throws InterruptedException { + int nThreads = 10; + List<Callable<String>> tasks = Collections.nCopies(nThreads, task); + ExecutorService executorService = Executors.newFixedThreadPool(nThreads); + List<Future<String>> futures = executorService.invokeAll(tasks); + + Set<String> ids = new HashSet<>(); + for (Future<String> future: futures) { + try { + ids.add(future.get()); + } catch (ExecutionException e) { + fail("unable to retrieve next id, " + e); + } + } + assertThat(ids.size(), is(nThreads)); + } +} \ No newline at end of file diff --git a/omega/omega-transport/pom.xml b/omega/omega-transport/pom.xml index ed501ff..7f2dc52 100644 --- a/omega/omega-transport/pom.xml +++ b/omega/omega-transport/pom.xml @@ -1,4 +1,22 @@ <?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + ~ + --> + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> @@ -10,17 +28,13 @@ <modelVersion>4.0.0</modelVersion> <artifactId>omega-transport</artifactId> + <packaging>pom</packaging> + <modules> + <module>omega-transport-resttemplate</module> + </modules> <dependencies> <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-core</artifactId> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-web</artifactId> - </dependency> - <dependency> <groupId>io.servicecomb.saga</groupId> <artifactId>omega-context</artifactId> </dependency> @@ -46,10 +60,6 @@ <artifactId>hamcrest-all</artifactId> </dependency> <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-test</artifactId> - </dependency> - <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-standalone</artifactId> </dependency> diff --git a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/RestTemplateConfig.java b/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/RestTemplateConfig.java deleted file mode 100644 index da2a66e..0000000 --- a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/RestTemplateConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.servicecomb.saga.omega.transport; - -import java.util.List; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.web.client.RestTemplate; - -@Configuration -public class RestTemplateConfig { - @Bean - public RestTemplate restTemplate() { - RestTemplate template = new RestTemplate(); - List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors(); - interceptors.add(new TransactionClientHttpRequestInterceptor()); - template.setInterceptors(interceptors); - return template; - } -} diff --git a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptor.java b/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptor.java deleted file mode 100644 index 7b2a34f..0000000 --- a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/TransactionClientHttpRequestInterceptor.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.servicecomb.saga.omega.transport; - -import java.io.IOException; - -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; - -import io.servicecomb.saga.core.IdGenerator; - -public class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { - - public static String TRANSACTION_ID_KEY = "X-Transaction-Id"; - - private IdGenerator<String> randomIdGenerator = new UniqueIdGenerator(); - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, - ClientHttpRequestExecution execution) throws IOException { - if (!request.getHeaders().containsKey(TRANSACTION_ID_KEY)) { - String txId = randomIdGenerator.nextId(); - request.getHeaders().add(TRANSACTION_ID_KEY, txId); - } - return execution.execute(request, body); - } -} diff --git a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/UniqueIdGenerator.java b/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/UniqueIdGenerator.java deleted file mode 100644 index fcf4c69..0000000 --- a/omega/omega-transport/src/main/java/io/servicecomb/saga/omega/transport/UniqueIdGenerator.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.servicecomb.saga.omega.transport; - -import java.util.UUID; - -import io.servicecomb.saga.core.IdGenerator; - -public class UniqueIdGenerator implements IdGenerator<String> { - @Override - public String nextId() { - return UUID.randomUUID().toString(); - } -} diff --git a/omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/UniqueIdGeneratorTest.java b/omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/UniqueIdGeneratorTest.java deleted file mode 100644 index 9e84b38..0000000 --- a/omega/omega-transport/src/test/java/io/servicecomb/saga/omega/transport/UniqueIdGeneratorTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.servicecomb.saga.omega.transport; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -import java.util.HashSet; -import java.util.Set; - -import org.junit.Test; - -public class UniqueIdGeneratorTest { - - private UniqueIdGenerator idGenerator = new UniqueIdGenerator(); - - @Test - public void nextIdIsUnique() { - Set<String> ids = new HashSet<>(); - for (int i = 0; i < 10; i++) { - String id = idGenerator.nextId(); - ids.add(id); - } - assertThat(ids.size(), is(10)); - } -} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
