bedrock-core is a toolkit based on vert.x for java application development, you can use it to easily create one or multi asynchronous and flexible tcp/udp/http server in your java application.
Add the following dependency to your pom.xml:
<dependency>
<groupId>com.gitee.rabbitnoteeth</groupId>
<artifactId>bedrock-core</artifactId>
<version>1.0.0</version>
</dependency>
Add the following dependency to your build.gradle file:
compile group: 'com.gitee.rabbitnoteeth', name: 'bedrock-core', version: '1.0.0'
TcpServerHandler
to handler these events: connect establish、connect disestablish、error、receive data.public class MyTcpServerHandler implements TcpServerHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MyTcpServerHandler.class);
@Override
public void onConnect(NetSocket socket) {
LOGGER.info("connect established");
}
@Override
public void onDisconnect(NetSocket socket) {
LOGGER.info("connect disestablished");
}
@Override
public void onError(Throwable e, NetSocket socket) {
LOGGER.error("error", e);
}
@Override
public void onData(Buffer buffer, NetSocket socket) {
LOGGER.error("receive data, {}", buffer.toString());
}
}
ServerDeploymentHandler
to handle these events: server start、server stop.public class MyTcpServerDeploymentHandler implements ServerDeploymentHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MyTcpServerDeploymentHandler.class);
@Override
public void onStart(String serverId) {
LOGGER.info("server start");
}
@Override
public void onStartFailed(String serverId, Throwable e) {
LOGGER.error("server start failed", e);
}
@Override
public void onStop(String serverId) {
LOGGER.info("server stop");
}
@Override
public void onStopFailed(String serverId, Throwable e) {
LOGGER.error("server stop failed", e);
}
}
// create options (this is optional)
NetServerOptions options = new NetServerOptions();
// config the options...
// create server
TcpServer server = new TcpServerBuilder(8080)
.setHandler(new MyTcpServerHandler())
.setDeploymentHandler(new MyTcpServerDeploymentHandler())
.setOptions(options)
.build();
// start server
server.start();
UcpServerHandler
to handler these events: connect establish、connect disestablish、error、receive data.public class MyUdpServerHandler implements UdpServerHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MyUdpServerHandler.class);
@Override
public void onConnect(DatagramSocket socket) {
LOGGER.info("connect established");
}
@Override
public void onDisconnect(DatagramSocket socket) {
LOGGER.info("connect disestablished");
}
@Override
public void onError(Throwable e, DatagramSocket socket) {
LOGGER.error("error", e);
}
@Override
public void onData(DatagramPacket packet, DatagramSocket socket) {
LOGGER.error("receive data, {}", packet.toString());
}
}
ServerDeploymentHandler
to handle these events: server start、server stop.public class MyUdpServerDeploymentHandler implements ServerDeploymentHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MyUdpServerDeploymentHandler.class);
@Override
public void onStart(String serverId) {
LOGGER.info("server start");
}
@Override
public void onStartFailed(String serverId, Throwable e) {
LOGGER.error("server start failed", e);
}
@Override
public void onStop(String serverId) {
LOGGER.info("server stop");
}
@Override
public void onStopFailed(String serverId, Throwable e) {
LOGGER.error("server stop failed", e);
}
}
// create options (this is optional)
DatagramSocketOptions options = new DatagramSocketOptions();
// config the options...
// create server
UdpServer server = new UdpServerBuilder(8080)
.setHandler(new MyUdpServerHandler())
.setDeploymentHandler(new MyTcpServerDeploymentHandler())
.setOptions(options)
.build();
// start server
server.start();
you may need the server return json data or html data (which rendered by a template engine such as thymeleaf
), well, both of them are supported in bedrock-core. Next I'll show you how to use it.
HttpRestRoute
(no methods are defined in this interface, it just means that your implement of it is a rest route) .@RoutePath("/demo")
public class RestRouteDemo implements HttpRestRoute {
@RoutePath("/test")
public String test(HttpContext httpContext,
HttpSession httpSession,
HttpRequestParams requestParams,
HttpRequestHeaders httpRequestHeaders,
HttpRequestBody httpRequestBody,
@RequestHeader("User-Agent") String userAgent,
@RequestParam("asd") String asd,
@RequestBody String xxx,
@RequestBody(attr = "aaa") String aaa,
@RequestBody(attr = "bbb", isArray = true) List<String> bbb,
@RequestData Aaa data) {
System.out.println("RequestHeader: User-Agent=" + userAgent);
System.out.println("RequestParam: asd=" + asd);
System.out.println("RequestBody: " + xxx);
System.out.println("RequestBody: aaa=" + aaa);
System.out.println("RequestBody: bbb=" + bbb);
System.out.println("RequestData: data=" + data);
return "ok";
}
}
in the RestRouteDemo
, the annotation @RoutePath
define the route path, in this demo, means the request of path /demo/test
will be handle by the method RestRouteDemo.test
.
if the type of the method parameter are HttpContext
、HttpSession
、HttpRequestParams
、HttpRequestHeaders
、HttpRequestBody
,the framework will create a instance of it automatically, you can get kinds of data in this request.
in addition, there are some annotations for the method parameter, such as @RequestHeader
、@RequestParam
、@RequestBody
、HttpRequestParams
、@RequestData
, you can use them to get the data from the request.
HttpRestRoute
(no methods are defined in this interface, it just means that your implement of it is a template route) .@RoutePath("/demo")
public class TemplateRouteDemo implements HttpTemplateRoute {
@RoutePath("/index")
public String index(HttpContext httpContext,
HttpSession httpSession,
HttpRequestParams requestParams,
HttpRequestHeaders httpRequestHeaders,
HttpRequestBody httpRequestBody,
@RequestHeader("User-Agent") String userAgent,
@RequestParam("asd") String asd,
@RequestBody String xxx,
@RequestBody(attr = "aaa") String aaa,
@RequestBody(attr = "bbb", isArray = true) List<String> bbb,
@RequestData Aaa data) {
System.out.println("RequestHeader: User-Agent=" + userAgent);
System.out.println("RequestParam: asd=" + asd);
System.out.println("RequestBody: " + xxx);
System.out.println("RequestBody: aaa=" + aaa);
System.out.println("RequestBody: bbb=" + bbb);
System.out.println("RequestData: data=" + data);
context.put("content", "this is index");
return "index";
}
}
in the TemplateRouteDemo
, the annotation @RoutePath
define the route path, in this demo, means the request of path /demo/index
will be handle by the method TemplateRouteDemo.index
, and this will return html data renderer by template index.html
.
if the type of the method parameter are HttpContext
、HttpSession
、HttpRequestParams
、HttpRequestHeaders
、HttpRequestBody
,the framework will create a instance of it automatically, you can get kinds of data in this request.
in addition, there are some annotations for the method parameter, such as @RequestHeader
、@RequestParam
、@RequestBody
、HttpRequestParams
、@RequestData
, you can use them to get the data from the request.
HttpServer server = new HttpServerBuilder(8090)
.setDeploymentOptions(deploymentOptions)
.setRestRoutes(List.of(new RestRouteDemo()))
.setTemplateRoutes(List.of(new TemplateRouteDemo()))
.build();
// start server
server.start();
thymeleaf
、 freemaker
, you can set by the method HttpServerBuilder.setTemplateEngine
, thymeleaf
is default.HttpServerBuilder.setErrorHandler
.HttpServerBuilder.setRequestHandlers
, the request handler will execute before your route, you can do some work in it like check whether requests are too frequent.HttpServerBuilder.setResponseHandlers
, the response handler will execute after your route, you can do some work in it like wrap the data in a uniform format.HttpServerBuilder.setRestRouteInterceptors
, you can do some work before or after the route, like check whether the user is logged in .Published under MIT License, see LICENSE
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。