Resource支持@name、@type

This commit is contained in:
redkale
2023-03-14 10:56:19 +08:00
parent 42201c7c11
commit 1edd256028
12 changed files with 245 additions and 76 deletions

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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++++同步方法");