提交 4c688701 编写于 作者: shi5470's avatar shi5470 提交者: wu-sheng

add SupplierWrapper to support java 1.8+ CompletableFuture.supplyAsync #3779 (#3783)

* add SupplierWrapper to support java 1.8+
Signed-off-by: Nshixiang <xiang.shi001@bkjk.com>

* add SupplierWrapper to support java 1.8+
Signed-off-by: Nshixiang <xiang.shi001@bkjk.com>

* Update SupplierWrapper.java

add new line

* add supplier

* add supplier test

* add test

* add test yaml

* Update Application-toolkit-trace-cross-thread.md

Add how to use SupplierWrapper in Java 1.8+
上级 39d18694
/*
* 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.skywalking.apm.toolkit.trace;
import java.util.function.Supplier;
/**
* @author sxzaihua
*/
@TraceCrossThread
public class SupplierWrapper<V> implements Supplier<V> {
final Supplier<V> supplier;
public static <V> SupplierWrapper of(Supplier<V> r) {
return new SupplierWrapper<V>(r);
}
public SupplierWrapper(Supplier<V> supplier) {
this.supplier = supplier;
}
@Override
public V get() {
return supplier.get();
}
}
......@@ -40,6 +40,7 @@ public class CallableOrRunnableActivation extends ClassInstanceMethodsEnhancePlu
private static final String CALL_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.CallableOrRunnableInvokeInterceptor";
private static final String CALL_METHOD_NAME = "call";
private static final String RUN_METHOD_NAME = "run";
private static final String GET_METHOD_NAME = "get";
@Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
......@@ -61,7 +62,8 @@ public class CallableOrRunnableActivation extends ClassInstanceMethodsEnhancePlu
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return (named(CALL_METHOD_NAME).and(takesArguments(0)))
.or(named(RUN_METHOD_NAME).and(takesArguments(0)));
.or(named(RUN_METHOD_NAME).and(takesArguments(0)))
.or(named(GET_METHOD_NAME).and(takesArguments(0)));
}
@Override public String getMethodsInterceptor() {
......
......@@ -39,7 +39,24 @@ or
}
}));
```
* usage 3.
```java
@TraceCrossThread
public class MySupplier<String> implements Supplier<String> {
@Override
public String get() {
return null;
}
}
...
CompletableFuture.supplyAsync(new MySupplier<String>());
```
or
```java
CompletableFuture.supplyAsync(SupplierWrapper.of(()->{
return "SupplierWrapper";
})).thenAccept(System.out::println);
```
_Sample codes only_
......
......@@ -22,7 +22,7 @@ registryItems:
operationNames:
- apm-toolkit-trace-scenario: [/apm-toolkit-trace-scenario/case/asyncVisit/runnable,
/case/asyncVisit/runnable, /case/asyncVisit/callable, /apm-toolkit-trace-scenario/case/asyncVisit/callable,
/case/tool-kit]
/case/tool-kit,/apm-toolkit-trace-scenario/case/asyncVisit/supplier,/case/asyncVisit/supplier]
heartbeat: []
segmentItems:
- applicationCode: apm-toolkit-trace-scenario
......@@ -259,6 +259,65 @@ segmentItems:
- {key: http.method, value: GET}
refs:
- {parentEndpointId: 0, parentEndpoint: Thread/org.apache.skywalking.apm.toolkit.trace.RunnableWrapper/run,
networkAddressId: 0, entryEndpointId: 0, refType: CrossProcess, parentSpanId: 1,
parentTraceSegmentId: not null, parentServiceInstanceId: 1,
networkAddress: 'localhost:8080', entryEndpoint: /case/tool-kit, entryServiceInstanceId: 1}
- segmentId: not null
spans:
- operationName: /apm-toolkit-trace-scenario/case/asyncVisit/supplier
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 2
componentName: ''
isError: false
spanType: Exit
peer: localhost:8080
peerId: 0
tags:
- {key: url, value: 'http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier'}
- {key: http.method, value: GET}
- operationName: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
componentName: ''
isError: false
spanType: Local
peer: ''
peerId: 0
refs:
- {parentEndpointId: 0, parentEndpoint: /case/tool-kit, networkAddressId: 0,
entryEndpointId: 0, refType: CrossThread, parentSpanId: 0, parentTraceSegmentId: not null,
parentServiceInstanceId: 1, networkAddress: '', entryEndpoint: /case/tool-kit,
entryServiceInstanceId: 1}
- segmentId: not null
spans:
- operationName: /case/asyncVisit/supplier
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 14
componentName: ''
isError: false
spanType: Entry
peer: ''
peerId: 0
tags:
- {key: url, value: 'http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier'}
- {key: http.method, value: GET}
refs:
- {parentEndpointId: 0, parentEndpoint: Thread/org.apache.skywalking.apm.toolkit.trace.SupplierWrapper/get,
networkAddressId: 0, entryEndpointId: 0, refType: CrossProcess, parentSpanId: 1,
parentTraceSegmentId: not null, parentServiceInstanceId: 1,
networkAddress: 'localhost:8080', entryEndpoint: /case/tool-kit, entryServiceInstanceId: 1}
\ No newline at end of file
/*
* 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.skywalking.apm.toolkit.trace;
import java.util.function.Supplier;
/**
* @author sxzaihua
*/
@TraceCrossThread
public class SupplierWrapper<V> implements Supplier<V> {
final Supplier<V> supplier;
public static <V> SupplierWrapper of(Supplier<V> r) {
return new SupplierWrapper<V>(r);
}
public SupplierWrapper(Supplier<V> supplier) {
this.supplier = supplier;
}
@Override
public V get() {
return supplier.get();
}
}
......@@ -62,6 +62,14 @@ public class TestController {
// ignore
}
});
testService.asyncSupplier(()->{
try {
visit("http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/supplier");
} catch (IOException e) {
// ignore
}
return true;
});
return SUCCESS;
}
......@@ -80,6 +88,11 @@ public class TestController {
return SUCCESS;
}
@RequestMapping("/asyncVisit/supplier")
public String asyncVisitSupplier() {
return SUCCESS;
}
private static void visit(String url) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
......
......@@ -21,12 +21,15 @@ package test.org.apache.skywalking.apm.testcase.toolkit.controller;
import org.apache.skywalking.apm.toolkit.trace.ActiveSpan;
import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
import org.apache.skywalking.apm.toolkit.trace.SupplierWrapper;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Component;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
/**
* @author caoyixiong
......@@ -78,4 +81,9 @@ public class TestService {
public void asyncCallable(Callable<Boolean> callable) {
SERVICE.submit(CallableWrapper.of(callable));
}
public void asyncSupplier(Supplier<Boolean> supplier) {
CompletableFuture.supplyAsync(SupplierWrapper.of(supplier));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册