ThrowSupplier

This commit is contained in:
redkale
2023-12-21 19:52:15 +08:00
parent 95b08e1db0
commit cfbf4bbe85
11 changed files with 357 additions and 110 deletions

View File

@@ -1,28 +0,0 @@
/*
*
*/
package org.redkale.test.cache;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author zhangjx
*/
public class BaseService {
private void run1() {
}
protected void run2() {
}
public void run3() {
}
public <T> Map<String, String> toMap(List<String> a, Set<T> set){
return null;
}
}

View File

@@ -0,0 +1,100 @@
/*
*
*/
package org.redkale.test.cache;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.redkale.cache.Cached;
import org.redkale.service.Service;
import org.redkale.source.Range;
import org.redkale.util.RedkaleException;
/**
*
* @author zhangjx
*/
public class CacheInstance implements Service {
@Cached(key = "name", localExpire = "30")
public String getName() {
return "haha";
}
@Cached(key = "name", localExpire = "30", remoteExpire = "60")
public String getName2() throws RedkaleException {
return "haha";
}
@Cached(key = "name", localExpire = "30")
public CompletableFuture<String> getNameAsync() {
return CompletableFuture.completedFuture("nameAsync");
}
@Cached(key = "name", localExpire = "30", remoteExpire = "60")
public CompletableFuture<String> getName2Async() throws IOException, InstantiationException {
return CompletableFuture.completedFuture("name2Async");
}
@Cached(key = "info_#{id}_file#{files.one}", localExpire = "30", remoteExpire = "60")
public File getInfo(ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return new File("aa.txt");
}
@Cached(key = "info_#{id}_file#{files.one}", localExpire = "30", remoteExpire = "60")
public CompletableFuture<File> getInfoAsync(ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return CompletableFuture.completedFuture(new File("aa.txt"));
}
@Cached(key = "info_#{id}_file#{files.one}", localExpire = "30", remoteExpire = "60")
public CompletableFuture<Map<String, Integer>> getInfo2Async(ParamBean bean, int id, List<String> idList, Map<String, File> files) throws IOException, InstantiationException {
return CompletableFuture.completedFuture(null);
}
public static class ParamBean {
private String name;
private int day;
private Integer status;
private Range.IntRange range;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Range.IntRange getRange() {
return range;
}
public void setRange(Range.IntRange range) {
this.range = range;
}
}
}

View File

@@ -0,0 +1,66 @@
/*
*
*/
package org.redkale.test.cache;
import java.net.InetSocketAddress;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.redkale.cache.CacheManager;
import org.redkale.cache.spi.CacheAsmMethodBoost;
import org.redkale.cache.spi.CacheManagerService;
import org.redkale.inject.ResourceFactory;
import org.redkale.net.AsyncGroup;
import org.redkale.net.client.ClientAddress;
import org.redkale.net.sncp.Sncp;
import org.redkale.net.sncp.SncpClient;
import org.redkale.net.sncp.SncpRpcGroups;
import org.redkale.source.CacheMemorySource;
import org.redkale.util.Environment;
import org.redkale.util.Utility;
/**
*
* @author zhangjx
*/
public class CacheInstanceTest {
private static ResourceFactory resourceFactory;
private static CacheManagerService manager;
public static void main(String[] args) throws Throwable {
CacheInstanceTest test = new CacheInstanceTest();
init();
test.run1();
test.run2();
}
@BeforeAll
public static void init() throws Exception {
resourceFactory = ResourceFactory.create();
resourceFactory.register(new Environment());
CacheMemorySource remoteSource = new CacheMemorySource("cache-remote");
remoteSource.init(null);
manager = CacheManagerService.create(remoteSource);
manager.init(null);
resourceFactory.register("", CacheManager.class, manager);
}
@Test
public void run1() throws Exception {
Class<CacheInstance> serviceClass = CacheInstance.class;
CacheAsmMethodBoost boost = new CacheAsmMethodBoost(serviceClass);
SncpRpcGroups grous = new SncpRpcGroups();
AsyncGroup iGroup = AsyncGroup.create("", Utility.newScheduledExecutor(1), 0, 0);
SncpClient client = new SncpClient("", iGroup, 0, new InetSocketAddress("127.0.0.1", 8080), new ClientAddress(), "TCP", 1, 16);
CacheInstance instance = Sncp.createLocalService(null, "", serviceClass, boost, resourceFactory,
grous, client, null, null, null);
System.out.println(instance.getName());
}
@Test
public void run2() throws Exception {
}
}

View File

@@ -22,7 +22,7 @@ public class CacheManagerTest {
public static void main(String[] args) throws Throwable {
CacheManagerTest test = new CacheManagerTest();
test.init();
init();
test.run1();
test.run2();
}

View File

@@ -1,13 +0,0 @@
/*
*/
package org.redkale.test.cache;
/**
*
* @author zhangjx
*/
public class SimpleService extends BaseService {
}

View File

@@ -1,45 +0,0 @@
/*
*
*/
package org.redkale.test.cache;
import java.lang.reflect.Method;
import org.redkale.asm.AsmMethodBoost;
import org.redkale.asm.Type;
/**
*
* @author zhangjx
*/
public class TwoService extends BaseService {
@Override
protected void run2() {
}
@Override
public void run3() {
}
public static void main(String[] args) throws Throwable {
System.out.println("-------------------------------");
for (Method m : TwoService.class.getDeclaredMethods()) {
System.out.println(m);
}
System.out.println("-------------------------------");
for (Method m : SimpleService.class.getDeclaredMethods()) {
System.out.println(m);
}
System.out.println("-------------------------------");
for (Method m : BaseService.class.getDeclaredMethods()) {
System.out.println(m);
if(m.getName().equals("toMap")) {
System.out.println("张颠三倒四: " + Type.getType(m).getInternalName());
System.out.println("张颠三倒四: " + Type.getType(m).getDescriptor());
System.out.println("张颠三倒四: " + Type.getType(m).getClassName());
}
}
System.out.println("-------------------------------");
System.out.println(AsmMethodBoost.getMethodBeans(BaseService.class));
}
}

View File

@@ -0,0 +1,124 @@
/*
*
*/
package org.redkale.test.cache;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.redkale.annotation.Resource;
import org.redkale.annotation.ResourceType;
import org.redkale.cache.spi.CacheAction;
import org.redkale.cache.spi.DynForCache;
import org.redkale.net.sncp.Sncp.SncpDyn;
import org.redkale.util.AnyValue;
import org.redkale.util.RedkaleException;
import org.redkale.util.ThrowSupplier;
@Resource(
name = ""
)
@SncpDyn(
remote = false,
type = CacheInstance.class
)
@ResourceType(CacheInstance.class)
public class _DynLocalCacheInstance extends CacheInstance {
private AnyValue _redkale_conf;
private String _redkale_mq;
private CacheAction _redkale_getNameCacheAction1;
private CacheAction _redkale_getInfoCacheAction2;
private CacheAction _redkale_getNameAsyncCacheAction3;
private CacheAction _redkale_getInfo2AsyncCacheAction4;
private CacheAction _redkale_getName2AsyncCacheAction5;
private CacheAction _redkale_getInfoAsyncCacheAction6;
private CacheAction _redkale_getName2CacheAction7;
public _DynLocalCacheInstance() {
}
@DynForCache(dynField = "_redkale_getNameCacheAction1", hash = "", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "-1", localExpire = "30"
)
public String getName() {
ThrowSupplier<String> supplier = () -> this.getName_afterCache();
return _redkale_getNameCacheAction1.get(supplier);
}
private String getName_afterCache() {
return super.getName();
}
@DynForCache(dynField = "_redkale_getInfoCacheAction2", hash = "", key = "info_#{id}_file#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", localExpire = "30")
public File getInfo(CacheInstance.ParamBean bean, int id, List<String> idList, Map<String, File> files) {
ThrowSupplier<File> supplier = () -> this.getInfo_afterCache(bean, id, idList, files);
return _redkale_getInfoCacheAction2.get(supplier);
}
private File getInfo_afterCache(CacheInstance.ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return super.getInfo(bean, id, idList, files);
}
@DynForCache(dynField = "_redkale_getNameAsyncCacheAction3", hash = "", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "-1", localExpire = "30")
public CompletableFuture<String> getNameAsync() {
ThrowSupplier<CompletableFuture<String>> supplier = () -> this.getNameAsync_afterCache();
return _redkale_getNameAsyncCacheAction3.get(supplier);
}
private CompletableFuture<String> getNameAsync_afterCache() {
return super.getNameAsync();
}
@DynForCache(dynField = "_redkale_getInfo2AsyncCacheAction4", hash = "", key = "info_#{id}_file#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", localExpire = "30")
public CompletableFuture<Map<String, Integer>> getInfo2Async(CacheInstance.ParamBean bean, int id,
List<String> idList, Map<String, File> files) throws IOException, InstantiationException {
ThrowSupplier<CompletableFuture<Map<String, Integer>>> supplier = () -> this.getInfo2Async_afterCache(bean, id, idList, files);
return _redkale_getInfo2AsyncCacheAction4.get(supplier, bean, id, idList, files);
}
private CompletableFuture<Map<String, Integer>> getInfo2Async_afterCache(CacheInstance.ParamBean bean, int id,
List<String> idList, Map<String, File> files) throws IOException, InstantiationException {
return super.getInfo2Async(bean, id, idList, files);
}
@DynForCache(dynField = "_redkale_getName2AsyncCacheAction5", hash = "", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", localExpire = "30")
public CompletableFuture<String> getName2Async() throws IOException, InstantiationException {
ThrowSupplier<CompletableFuture<String>> supplier = () -> this.getName2Async_afterCache();
return _redkale_getName2AsyncCacheAction5.get(supplier);
}
private CompletableFuture<String> getName2Async_afterCache() throws IOException, InstantiationException {
return super.getName2Async();
}
@DynForCache(dynField = "_redkale_getInfoAsyncCacheAction6", hash = "", key = "info_#{id}_file#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", localExpire = "30")
public CompletableFuture<File> getInfoAsync(CacheInstance.ParamBean bean, int id, List<String> idList, Map<String, File> files) {
ThrowSupplier<CompletableFuture<File>> supplier = () -> this.getInfoAsync_afterCache(bean, id, idList, files);
return _redkale_getInfoAsyncCacheAction6.get(supplier, bean, id, idList, files);
}
private CompletableFuture<File> getInfoAsync_afterCache(CacheInstance.ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return super.getInfoAsync(bean, id, idList, files);
}
@DynForCache(dynField = "_redkale_getName2CacheAction7", hash = "", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", localExpire = "30")
public String getName2() throws RedkaleException {
ThrowSupplier<String> supplier = () -> this.getName2_afterCache();
return _redkale_getName2CacheAction7.get(supplier);
}
private String getName2_afterCache() throws RedkaleException {
return super.getName2();
}
}