Resource支持@name、@type
This commit is contained in:
@@ -8,6 +8,10 @@ package org.redkale.annotation;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @Resource(name = "$") 表示资源name采用所属对象的name <br>
|
||||
* @Resource(name = "@name") 表示资源对象自身的name <br>
|
||||
* @Resource(name = "@type") 表示资源对象自身的类型 <br>
|
||||
*
|
||||
* @since Common Annotations 1.0
|
||||
*
|
||||
* @since 2.8.0
|
||||
@@ -15,7 +19,7 @@ import java.lang.annotation.*;
|
||||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Resource {
|
||||
|
||||
|
||||
/**
|
||||
* 是否必须存在
|
||||
*
|
||||
@@ -26,7 +30,13 @@ public @interface Resource {
|
||||
public boolean required() default true;
|
||||
|
||||
/**
|
||||
* 资源名称
|
||||
* 资源名称 <br>
|
||||
* <blockquote><pre>
|
||||
* name规则:
|
||||
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
|
||||
* 2: "@name"、"@type"有特殊含义
|
||||
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
|
||||
@@ -67,9 +67,9 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
protected void putServlet(S servlet) {
|
||||
servletLock.lock();
|
||||
try {
|
||||
Set<S> newservlets = new HashSet<>(servlets);
|
||||
newservlets.add(servlet);
|
||||
this.servlets = newservlets;
|
||||
Set<S> newServlets = new HashSet<>(servlets);
|
||||
newServlets.add(servlet);
|
||||
this.servlets = newServlets;
|
||||
} finally {
|
||||
servletLock.unlock();
|
||||
}
|
||||
@@ -78,9 +78,9 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
protected void removeServlet(S servlet) {
|
||||
servletLock.lock();
|
||||
try {
|
||||
Set<S> newservlets = new HashSet<>(servlets);
|
||||
newservlets.remove(servlet);
|
||||
this.servlets = newservlets;
|
||||
Set<S> newServlets = new HashSet<>(servlets);
|
||||
newServlets.remove(servlet);
|
||||
this.servlets = newServlets;
|
||||
doAfterRemove(servlet);
|
||||
} finally {
|
||||
servletLock.unlock();
|
||||
@@ -118,9 +118,9 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
protected void putMapping(K key, S servlet) {
|
||||
mappingLock.lock();
|
||||
try {
|
||||
Map<K, S> newmappings = new HashMap<>(mappings);
|
||||
newmappings.put(key, servlet);
|
||||
this.mappings = newmappings;
|
||||
Map<K, S> newMappings = new HashMap<>(mappings);
|
||||
newMappings.put(key, servlet);
|
||||
this.mappings = newMappings;
|
||||
} finally {
|
||||
mappingLock.unlock();
|
||||
}
|
||||
@@ -144,14 +144,14 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
mappingLock.lock();
|
||||
try {
|
||||
List<K> keys = new ArrayList<>();
|
||||
Map<K, S> newmappings = new HashMap<>(mappings);
|
||||
for (Map.Entry<K, S> en : newmappings.entrySet()) {
|
||||
Map<K, S> newMappings = new HashMap<>(mappings);
|
||||
for (Map.Entry<K, S> en : newMappings.entrySet()) {
|
||||
if (en.getValue().equals(servlet)) {
|
||||
keys.add(en.getKey());
|
||||
}
|
||||
}
|
||||
for (K key : keys) newmappings.remove(key);
|
||||
this.mappings = newmappings;
|
||||
for (K key : keys) newMappings.remove(key);
|
||||
this.mappings = newMappings;
|
||||
doAfterRemove(servlet);
|
||||
} finally {
|
||||
mappingLock.unlock();
|
||||
@@ -176,7 +176,7 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
if (!filters.isEmpty()) {
|
||||
Collections.sort(filters);
|
||||
for (Filter<C, R, P> filter : filters) {
|
||||
filter.init(context, config);
|
||||
filter.init(context, filter._conf);
|
||||
}
|
||||
this.headFilter = filters.get(0);
|
||||
Filter<C, R, P> filter = this.headFilter;
|
||||
@@ -191,13 +191,13 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("unchecked") //Servlet由子类来销毁
|
||||
public void destroy(C context, AnyValue config) {
|
||||
filtersLock.lock();
|
||||
try {
|
||||
if (!filters.isEmpty()) {
|
||||
for (Filter filter : filters) {
|
||||
filter.destroy(context, config);
|
||||
filter.destroy(context, filter._conf);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -307,6 +307,14 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
||||
servlet._conf = conf;
|
||||
}
|
||||
|
||||
protected AnyValue getFilterConf(Filter filter) {
|
||||
return filter._conf;
|
||||
}
|
||||
|
||||
protected void setFilterConf(Filter filter, AnyValue conf) {
|
||||
filter._conf = conf;
|
||||
}
|
||||
|
||||
public List<S> getServlets() {
|
||||
return new ArrayList<>(servlets);
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ package org.redkale.net;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.*;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import org.redkale.boot.Application;
|
||||
import javax.net.ssl.*;
|
||||
import org.redkale.boot.*;
|
||||
import static org.redkale.net.AsyncGroup.UDP_BUFFER_CAPACITY;
|
||||
import org.redkale.net.Filter;
|
||||
import org.redkale.util.*;
|
||||
@@ -483,6 +483,16 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
|
||||
return this.dispatcher.containsFilter(filterClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁Servlet
|
||||
*
|
||||
* @param <T> 泛型
|
||||
* @param filter Filter
|
||||
*/
|
||||
public <T extends Filter> void destroyFilter(T filter) {
|
||||
filter.destroy(context, this.dispatcher.getFilterConf(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否存在Servlet
|
||||
*
|
||||
|
||||
@@ -5,21 +5,22 @@
|
||||
*/
|
||||
package org.redkale.net.sncp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.annotation.NonBlocking;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.annotation.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
import org.redkale.asm.*;
|
||||
import static org.redkale.asm.Opcodes.*;
|
||||
import org.redkale.asm.Type;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.convert.Reader;
|
||||
import org.redkale.convert.bson.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -159,11 +160,12 @@ public final class SncpDynServlet extends SncpServlet {
|
||||
java.lang.reflect.Type handlerResultType = null;
|
||||
try {
|
||||
final Class[] paramClasses = method.getParameterTypes();
|
||||
java.lang.reflect.Type[] genericParams = method.getGenericParameterTypes();
|
||||
for (int i = 0; i < paramClasses.length; i++) { //反序列化方法的每个参数
|
||||
if (CompletionHandler.class.isAssignableFrom(paramClasses[i])) {
|
||||
handlerFuncIndex = i;
|
||||
handlerFuncClass = paramClasses[i];
|
||||
java.lang.reflect.Type handlerType = TypeToken.getGenericType(method.getTypeParameters()[i], service.getClass());
|
||||
java.lang.reflect.Type handlerType = TypeToken.getGenericType(genericParams[i], service.getClass());
|
||||
if (handlerType instanceof Class) {
|
||||
handlerResultType = Object.class;
|
||||
} else if (handlerType instanceof ParameterizedType) {
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
*/
|
||||
package org.redkale.net.sncp;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.mq.*;
|
||||
import static org.redkale.net.sncp.Sncp.loadMethodActions;
|
||||
import org.redkale.service.*;
|
||||
@@ -185,6 +185,7 @@ public final class SncpServiceInfo<T extends Service> {
|
||||
Class handlerFuncClass = null;
|
||||
java.lang.reflect.Type handlerResultType = null;
|
||||
Class<?>[] params = method.getParameterTypes();
|
||||
Type[] genericParams = method.getGenericParameterTypes();
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
if (CompletionHandler.class.isAssignableFrom(params[i])) {
|
||||
if (Future.class.isAssignableFrom(method.getReturnType())) {
|
||||
@@ -196,7 +197,7 @@ public final class SncpServiceInfo<T extends Service> {
|
||||
Sncp.checkAsyncModifier(params[i], method);
|
||||
handlerFuncIndex = i;
|
||||
handlerFuncClass = paramClasses[i];
|
||||
java.lang.reflect.Type handlerType = TypeToken.getGenericType(method.getTypeParameters()[i], serviceImplClass);
|
||||
java.lang.reflect.Type handlerType = TypeToken.getGenericType(genericParams[i], serviceImplClass);
|
||||
if (handlerType instanceof Class) {
|
||||
handlerResultType = Object.class;
|
||||
} else if (handlerType instanceof ParameterizedType) {
|
||||
|
||||
@@ -9,8 +9,7 @@ import java.util.concurrent.*;
|
||||
import org.redkale.annotation.Resource;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.net.WorkThread;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
import org.redkale.util.ThreadHashExecutor;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -22,13 +21,23 @@ public abstract class AbstractService implements Service {
|
||||
@Resource(name = Application.RESNAME_APP_EXECUTOR, required = false)
|
||||
private ExecutorService workExecutor;
|
||||
|
||||
@Resource(name = ResourceFactory.RESOURCE_SELF_NAME, required = false)
|
||||
private String serviceName;
|
||||
|
||||
@Resource(name = ResourceFactory.RESOURCE_SELF_TYPE, required = false)
|
||||
private Class serviceType;
|
||||
|
||||
protected String serviceName() {
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前Service类的原始Service类型, 由于Service会动态重载,所以getClass()得到的不是原始Service类型
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
protected Class serviceType() {
|
||||
return Sncp.isSncpDyn(this) && getClass().getSimpleName().startsWith("_Dyn") ? getClass().getSuperclass() : getClass();
|
||||
return serviceType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.ref.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.math.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.concurrent.locks.*;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.annotation.*;
|
||||
@@ -28,8 +28,9 @@ import org.redkale.convert.*;
|
||||
* 如果没有@Resource且对象实现了Resourcable, 则会取对象的resourceName()方法值
|
||||
* <blockquote><pre>
|
||||
* name规则:
|
||||
* 1: "$"有特殊含义, 不能表示"$"资源本身
|
||||
* 2: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
|
||||
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
|
||||
* 2: "@name"、"@type"有特殊含义
|
||||
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
|
||||
* </pre></blockquote>
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
@@ -41,6 +42,10 @@ public final class ResourceFactory {
|
||||
|
||||
public static final String RESOURCE_PARENT_NAME = "$";
|
||||
|
||||
public static final String RESOURCE_SELF_NAME = "@name";
|
||||
|
||||
public static final String RESOURCE_SELF_TYPE = "@type";
|
||||
|
||||
private static final boolean skipCheckRequired = Boolean.getBoolean("redkale.resource.skip.check");
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ResourceFactory.class.getSimpleName());
|
||||
@@ -128,7 +133,8 @@ public final class ResourceFactory {
|
||||
* <blockquote><pre>
|
||||
* name规则:
|
||||
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
|
||||
* 2: 只能是字母、数字、(短横)-、(下划线)_、点(.)、小括号、中括号的组合
|
||||
* 2: "@name"、"@type"有特殊含义
|
||||
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* @param name String
|
||||
@@ -544,6 +550,58 @@ public final class ResourceFactory {
|
||||
if (clz != null && !clz.isPrimitive() && val != null && !clz.isAssignableFrom(val.getClass())) {
|
||||
throw new RedkaleException(clz + "not isAssignableFrom (" + val + ") class " + val.getClass());
|
||||
}
|
||||
String clzname = clz != null ? clz.getName() : "java.lang.String";
|
||||
if (val != null && !clzname.startsWith("java.") && !clzname.startsWith("javax.")
|
||||
&& !clzname.startsWith("jdk.") && !clzname.startsWith("sun.")) {
|
||||
Class c = val.getClass();
|
||||
do {
|
||||
if (java.lang.Enum.class.isAssignableFrom(c)) {
|
||||
break;
|
||||
}
|
||||
final String cname = c.getName();
|
||||
if (cname.startsWith("java.") || cname.startsWith("javax.")
|
||||
|| cname.startsWith("jdk.") || cname.startsWith("sun.")) {
|
||||
break;
|
||||
}
|
||||
if (cname.indexOf('/') < 0) {//排除内部类, 如:JsonConvert$$Lambda$87/0x0000000100197440-
|
||||
RedkaleClassLoader.putReflectionDeclaredFields(cname);
|
||||
}
|
||||
for (Field field : c.getDeclaredFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (Modifier.isFinal(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
Resource rc = field.getAnnotation(Resource.class);
|
||||
if (rc == null) {
|
||||
continue;
|
||||
}
|
||||
if (!rc.name().equals(RESOURCE_SELF_NAME) && !rc.name().equals(RESOURCE_SELF_TYPE)) {
|
||||
continue;
|
||||
}
|
||||
final Class classType = field.getType();
|
||||
RedkaleClassLoader.putReflectionField(cname, field);
|
||||
try {
|
||||
if (rc.name().equals(RESOURCE_SELF_NAME)) {
|
||||
if (classType != String.class) {
|
||||
throw new ResourceInjectException("resource(type=" + c.getSimpleName() + ".class, field=" + field.getName() + ", name='" + rc.name() + "') must be String ");
|
||||
}
|
||||
field.setAccessible(true);
|
||||
field.set(val, name);
|
||||
} else if (rc.name().equals(RESOURCE_SELF_TYPE)) {
|
||||
if (classType != Type.class && classType != Class.class) {
|
||||
throw new ResourceInjectException("resource(type=" + c.getSimpleName() + ".class, field=" + field.getName() + ", name='" + rc.name() + "') must be Type or Class ");
|
||||
}
|
||||
field.setAccessible(true);
|
||||
field.set(val, classType == Class.class ? TypeToken.typeToClass(clazz) : clazz);
|
||||
}
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
throw new ResourceInjectException("resource(type=" + c.getSimpleName() + ".class, field=" + field.getName() + ", name='" + rc.name() + "') register error ", e);
|
||||
}
|
||||
}
|
||||
} while ((c = c.getSuperclass()) != Object.class);
|
||||
}
|
||||
ConcurrentHashMap<String, ResourceEntry> map = this.store.computeIfAbsent(clazz, k -> new ConcurrentHashMap());
|
||||
ResourceEntry re = map.get(name);
|
||||
if (re == null) {
|
||||
@@ -828,6 +886,9 @@ public final class ResourceFactory {
|
||||
consumer.accept(srcObj, field);
|
||||
}
|
||||
String tname = rc1 == null ? rc2.name() : rc1.name();
|
||||
if (tname.equals(RESOURCE_SELF_NAME) || tname.equals(RESOURCE_SELF_TYPE)) {
|
||||
continue;
|
||||
}
|
||||
if (tname.contains(RESOURCE_PARENT_NAME)) {
|
||||
Resource res1 = srcObj.getClass().getAnnotation(Resource.class);
|
||||
javax.annotation.Resource res2 = srcObj.getClass().getAnnotation(javax.annotation.Resource.class);
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -15,8 +15,12 @@ import org.redkale.convert.json.JsonConvert;
|
||||
*/
|
||||
public class DyncJsonTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new DyncJsonTest().run();
|
||||
DyncJsonTest test = new DyncJsonTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -24,14 +28,17 @@ public class DyncJsonTest {
|
||||
SimpleDyncBean bean = new SimpleDyncBean();
|
||||
bean.name = "haha";
|
||||
System.out.println(JsonConvert.root().convertTo(bean));
|
||||
if (!main) Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean));
|
||||
|
||||
SimpleDyncBean2 bean2 = new SimpleDyncBean2();
|
||||
bean2.name = "haha";
|
||||
|
||||
System.out.println(JsonConvert.root().convertTo(bean2));
|
||||
if (!main) Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean2));
|
||||
SimpleDyncBean3 bean3 = new SimpleDyncBean3();
|
||||
bean3.name = "haha";
|
||||
System.out.println(JsonConvert.root().convertTo(bean3));
|
||||
if (!main) Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean3));
|
||||
}
|
||||
|
||||
public static class SimpleDyncBean {
|
||||
|
||||
@@ -4,7 +4,7 @@ package org.redkale.test.convert;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.json.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -30,7 +30,9 @@ public class Json5Test {
|
||||
bean.name = "haha";
|
||||
String json = "{/*多行\r\n注释**/\"decmails\":3.2,//单行注释\r\n\"id\":0x1F4,\"name\":\"haha\",\"value\":44444,}";
|
||||
Json5Bean bean2 = convert.convertFrom(Json5Bean.class, json);
|
||||
if (!main) Assertions.assertTrue(bean.equals(bean2));
|
||||
if (!main) {
|
||||
Assertions.assertTrue(bean.equals(bean2));
|
||||
}
|
||||
System.out.println(convert.convertTo(bean2));
|
||||
|
||||
String arrayJson = "[" + json + "," + json + "," + "]";
|
||||
@@ -64,21 +66,28 @@ public class Json5Test {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
if (this == obj) {
|
||||
return true;
|
||||
if (obj == null)
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Json5Bean other = (Json5Bean) obj;
|
||||
if (this.id != other.id)
|
||||
if (this.id != other.id) {
|
||||
return false;
|
||||
if (Float.floatToIntBits(this.decmails) != Float.floatToIntBits(other.decmails))
|
||||
}
|
||||
if (Float.floatToIntBits(this.decmails) != Float.floatToIntBits(other.decmails)) {
|
||||
return false;
|
||||
if (this.value != other.value)
|
||||
}
|
||||
if (this.value != other.value) {
|
||||
return false;
|
||||
if (!Objects.equals(this.name, other.name))
|
||||
}
|
||||
if (!Objects.equals(this.name, other.name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,20 +5,21 @@
|
||||
*/
|
||||
package org.redkale.test.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.*;
|
||||
import org.redkale.annotation.Resource;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.annotation.*;
|
||||
import org.redkale.boot.*;
|
||||
import org.redkale.convert.bson.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.net.sncp.*;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
import org.redkale.util.*;
|
||||
|
||||
@@ -32,8 +33,9 @@ public class ABMainService implements Service {
|
||||
@Resource
|
||||
private BCService bcService;
|
||||
|
||||
public static void remotemain(String[] args) throws Throwable {
|
||||
public static void remote(String[] args) throws Throwable {
|
||||
System.out.println("------------------- 远程模式调用 -----------------------------------");
|
||||
final Application application = Application.create(true);
|
||||
final int abport = 8888;
|
||||
final AsyncIOGroup asyncGroup = new AsyncIOGroup(8192, 16);
|
||||
asyncGroup.start();
|
||||
@@ -50,6 +52,7 @@ public class ABMainService implements Service {
|
||||
//------------------------ 初始化 CService ------------------------------------
|
||||
CService cservice = Sncp.createSimpleLocalService(CService.class, null, resFactory, transFactory, new InetSocketAddress("127.0.0.1", 5577), "g77");
|
||||
SncpServer cserver = new SncpServer();
|
||||
cserver.getResourceFactory().register(application);
|
||||
cserver.getLogger().setLevel(Level.WARNING);
|
||||
cserver.addSncpServlet(cservice);
|
||||
cserver.init(DefaultAnyValue.create("port", 5577));
|
||||
@@ -61,6 +64,7 @@ public class ABMainService implements Service {
|
||||
resFactory.inject(remoteCService);
|
||||
resFactory.register("", remoteCService);
|
||||
SncpServer bcserver = new SncpServer();
|
||||
bcserver.getResourceFactory().register(application);
|
||||
bcserver.getLogger().setLevel(Level.WARNING);
|
||||
bcserver.addSncpServlet(bcservice);
|
||||
bcserver.init(DefaultAnyValue.create("port", 5588));
|
||||
@@ -73,6 +77,7 @@ public class ABMainService implements Service {
|
||||
resFactory.register("", remoteBCService);
|
||||
|
||||
HttpServer server = new HttpServer();
|
||||
server.getResourceFactory().register(application);
|
||||
server.getLogger().setLevel(Level.WARNING);
|
||||
|
||||
server.addRestServlet(null, service, null, HttpServlet.class, "/pipes");
|
||||
@@ -84,7 +89,7 @@ public class ABMainService implements Service {
|
||||
server.init(DefaultAnyValue.create("port", abport));
|
||||
server.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
System.out.println("开始请求");
|
||||
//同步方法
|
||||
String url = "http://127.0.0.1:" + abport + "/pipes/abmain/syncabtime/张先生";
|
||||
System.out.println(Utility.postHttpContent(url));
|
||||
@@ -102,6 +107,7 @@ public class ABMainService implements Service {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
System.out.println("------------------- 本地模式调用 -----------------------------------");
|
||||
final Application application = Application.create(true);
|
||||
final int abport = 8888;
|
||||
ResourceFactory factory = ResourceFactory.create();
|
||||
|
||||
@@ -112,8 +118,11 @@ public class ABMainService implements Service {
|
||||
factory.register("", new CService());
|
||||
factory.inject(bcservice);
|
||||
factory.inject(service);
|
||||
System.out.println("bcservice.name = " + bcservice.serviceName());
|
||||
System.out.println("bcservice.type = " + bcservice.serviceType());
|
||||
|
||||
HttpServer server = new HttpServer();
|
||||
server.getResourceFactory().register(application);
|
||||
server.getLogger().setLevel(Level.WARNING);
|
||||
|
||||
server.addRestServlet(null, service, null, HttpServlet.class, "/pipes");
|
||||
@@ -136,7 +145,7 @@ public class ABMainService implements Service {
|
||||
|
||||
server.shutdown();
|
||||
//远程模式
|
||||
remotemain(args);
|
||||
remote(args);
|
||||
}
|
||||
|
||||
public static AsynchronousChannelGroup newChannelGroup() throws IOException {
|
||||
@@ -153,7 +162,9 @@ public class ABMainService implements Service {
|
||||
public static ObjectPool<ByteBuffer> newBufferPool() {
|
||||
return ObjectPool.createSafePool(new LongAdder(), new LongAdder(), 16,
|
||||
(Object... params) -> ByteBuffer.allocateDirect(8192), null, (e) -> {
|
||||
if (e == null || e.isReadOnly() || e.capacity() != 8192) return false;
|
||||
if (e == null || e.isReadOnly() || e.capacity() != 8192) {
|
||||
return false;
|
||||
}
|
||||
e.clear();
|
||||
return true;
|
||||
});
|
||||
@@ -171,9 +182,13 @@ public class ABMainService implements Service {
|
||||
bcService.bcCurrentTime(Utility.createAsyncHandler((v, a) -> {
|
||||
System.out.println("执行了 ABMainService.abCurrentTime----异步方法");
|
||||
String rs = "异步abCurrentTime: " + v;
|
||||
if (handler != null) handler.completed(rs, a);
|
||||
if (handler != null) {
|
||||
handler.completed(rs, a);
|
||||
}
|
||||
}, (t, a) -> {
|
||||
if (handler != null) handler.failed(t, a);
|
||||
if (handler != null) {
|
||||
handler.failed(t, a);
|
||||
}
|
||||
}), name);
|
||||
}
|
||||
|
||||
@@ -189,7 +204,9 @@ public class ABMainService implements Service {
|
||||
public void completed(String v, Void a) {
|
||||
System.out.println("执行了 ABMainService.abCurrentTime----异步方法2");
|
||||
String rs = "异步abCurrentTime: " + v;
|
||||
if (handler != null) handler.completed(rs, a);
|
||||
if (handler != null) {
|
||||
handler.completed(rs, a);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
*/
|
||||
package org.redkale.test.service;
|
||||
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import org.redkale.annotation.Resource;
|
||||
import java.nio.channels.*;
|
||||
import org.redkale.annotation.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
@@ -19,6 +19,20 @@ public class BCService implements Service {
|
||||
@Resource
|
||||
private CService cService;
|
||||
|
||||
@Resource(name = "@name")
|
||||
private String serviceName;
|
||||
|
||||
@Resource(name = "@type")
|
||||
private Class serviceType;
|
||||
|
||||
public String serviceName() {
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
public Class serviceType() {
|
||||
return serviceType;
|
||||
}
|
||||
|
||||
public String bcCurrentTime(final String name) {
|
||||
String rs = "同步bcCurrentTime: " + cService.ccCurrentTime(name).getResult();
|
||||
System.out.println("执行了 BCService.bcCurrentTime++++同步方法");
|
||||
@@ -29,9 +43,13 @@ public class BCService implements Service {
|
||||
cService.ccCurrentTime(Utility.createAsyncHandler((v, a) -> {
|
||||
System.out.println("执行了 BCService.bcCurrentTime----异步方法");
|
||||
String rs = "异步bcCurrentTime: " + (v == null ? null : v.getResult());
|
||||
if (handler != null) handler.completed(rs, null);
|
||||
if (handler != null) {
|
||||
handler.completed(rs, null);
|
||||
}
|
||||
}, (t, a) -> {
|
||||
if (handler != null) handler.failed(t, a);
|
||||
if (handler != null) {
|
||||
handler.failed(t, a);
|
||||
}
|
||||
}), name);
|
||||
}
|
||||
|
||||
@@ -46,7 +64,9 @@ public class BCService implements Service {
|
||||
public void completed(RetResult<String> v, Void a) {
|
||||
System.out.println("执行了 BCService.bcCurrentTime----异步方法2");
|
||||
String rs = "异步bcCurrentTime: " + (v == null ? null : v.getResult());
|
||||
if (handler != null) handler.completed(rs, null);
|
||||
if (handler != null) {
|
||||
handler.completed(rs, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
*/
|
||||
package org.redkale.test.service;
|
||||
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.channels.*;
|
||||
import org.redkale.annotation.*;
|
||||
import org.redkale.service.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
@@ -15,6 +16,20 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public class CService implements Service {
|
||||
|
||||
@Resource(name = "@name")
|
||||
private String serviceName;
|
||||
|
||||
@Resource(name = "@type")
|
||||
private Class serviceType;
|
||||
|
||||
public String serviceName() {
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
public Class serviceType() {
|
||||
return serviceType;
|
||||
}
|
||||
|
||||
public RetResult<String> ccCurrentTime(final String name) {
|
||||
String rs = "同步ccCurrentTime: " + name + ": " + Utility.formatTime(System.currentTimeMillis());
|
||||
System.out.println("执行了 CService.ccCurrentTime++++同步方法");
|
||||
|
||||
Reference in New Issue
Block a user