Browse Source

[Java/baseio] update to 3.2.9-bata-1 (#4216)

* * tfb test for baseio

* fix word spell

* * add new test baseio to .travis.yml

* Update README.md

* * add json test case

*  * update baseio version to 3.2.9-BETA-1
Kai Wang 6 years ago
parent
commit
59d93f8c52

+ 1 - 1
frameworks/Java/baseio/pom.xml

@@ -19,7 +19,7 @@
 		<dependency>
 			<groupId>com.generallycloud</groupId>
 			<artifactId>baseio-all</artifactId>
-			<version>3.2.8.RELEASE</version>
+			<version>3.2.9-BETA-1</version>
 		</dependency>
 		
 		<dependency>

+ 43 - 41
frameworks/Java/baseio/src/main/java/hello/TestHttpLoadServer.java

@@ -16,16 +16,15 @@
 package hello;
 
 import com.alibaba.fastjson.JSON;
-import com.generallycloud.baseio.Constants;
+import com.generallycloud.baseio.Options;
 import com.generallycloud.baseio.buffer.ByteBuf;
+import com.generallycloud.baseio.codec.http11.HttpCodec;
+import com.generallycloud.baseio.codec.http11.HttpFrame;
 import com.generallycloud.baseio.codec.http11.HttpHeader;
 import com.generallycloud.baseio.codec.http11.HttpStatic;
 import com.generallycloud.baseio.codec.http11.HttpStatus;
-import com.generallycloud.baseio.codec.http11.ServerHttpCodec;
-import com.generallycloud.baseio.codec.http11.ServerHttpFrame;
 import com.generallycloud.baseio.common.Encoding;
 import com.generallycloud.baseio.component.ChannelAcceptor;
-import com.generallycloud.baseio.component.ChannelContext;
 import com.generallycloud.baseio.component.IoEventHandle;
 import com.generallycloud.baseio.component.NioEventLoopGroup;
 import com.generallycloud.baseio.component.NioSocketChannel;
@@ -33,62 +32,65 @@ import com.generallycloud.baseio.log.LoggerFactory;
 import com.generallycloud.baseio.protocol.Frame;
 
 public class TestHttpLoadServer {
-    
+
     static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(Encoding.UTF8);
-    static final byte[] CONTENT_TYPE_JSON = "application/json".getBytes(Encoding.UTF8);
-	
+    static final byte[] STATIC_SERVER    = "baseio".getBytes();
+
     public static void main(String[] args) throws Exception {
-    	LoggerFactory.setLogLevel(LoggerFactory.LEVEL_ERROR);
-    	System.setProperty(Constants.DEVELOP_DEBUG_KEY, "false");
-        
+        LoggerFactory.setEnableSLF4JLogger(false);
+        LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
+        Options.setDevelopDebug(false);
+        Options.setChannelReadFirst(true);
+
         IoEventHandle eventHandle = new IoEventHandle() {
 
             @Override
             public void accept(NioSocketChannel ch, Frame frame) throws Exception {
-            	ServerHttpFrame f = (ServerHttpFrame) frame;
-            	String action = f.getRequestURI();
-            	f.setResponseHeader(HttpHeader.Connection_Bytes, null);
-                
-            	if("/plaintext".equals(action)){
-            		frame.write(STATIC_PLAINTEXT);
-            		f.setResponseHeader(HttpHeader.Content_Type_Bytes, HttpStatic.plain_bytes);
-            	}else if("/json".equals(action)){
-            		frame.write(JSON.toJSONString(new Message("Hello, World!")), ch);
-            		f.setResponseHeader(HttpHeader.Content_Type_Bytes, CONTENT_TYPE_JSON);
-            	}else{
-            		frame.write("404,page not found!",ch);
-            		f.setStatus(HttpStatus.C404);
-            	}
-                ByteBuf buf = ch.encode(frame);
+                HttpFrame f = (HttpFrame) frame;
+                String action = f.getRequestURI();
+                f.getResponseHeaders().remove(HttpHeader.Connection);
+                f.setResponseHeader(HttpHeader.Server, STATIC_SERVER);
+
+                if ("/plaintext".equals(action)) {
+                    f.write(STATIC_PLAINTEXT);
+                    f.setResponseHeader(HttpHeader.Content_Type, HttpStatic.text_plain_bytes);
+                } else if ("/json".equals(action)) {
+                    f.write(JSON.toJSONString(new Message("Hello, World!")), ch);
+                    f.setResponseHeader(HttpHeader.Content_Type, HttpStatic.application_json_bytes);
+                } else {
+                    f.write("404,page not found!", ch);
+                    f.setStatus(HttpStatus.C404);
+                }
+                ByteBuf buf = ch.encode(f);
                 ch.flush(buf);
-                f.release(ch.getEventLoop());
+                ch.release(f);
             }
 
         };
 
+        int core_size = Runtime.getRuntime().availableProcessors();
         NioEventLoopGroup group = new NioEventLoopGroup();
-        group.setMemoryPoolCapacity(1024 * 256);
+        group.setMemoryPoolCapacity(1024 * 1024 / core_size);
         group.setMemoryPoolUnit(512);
-        ChannelContext context = new ChannelContext(8080);
-        ChannelAcceptor acceptor = new ChannelAcceptor(context, group);
-        context.setProtocolCodec(new ServerHttpCodec(1024 * 8));
+        group.setEventLoopSize(core_size);
+        ChannelAcceptor context = new ChannelAcceptor(group, 8080);
+        context.setProtocolCodec(new HttpCodec(1024 * 16));
         context.setIoEventHandle(eventHandle);
-
-        acceptor.bind();
+        context.bind();
     }
-    
+
     static class Message {
 
-    	private final String message;
+        private final String message;
 
-    	public Message(String message) {
-    		this.message = message;
-    	}
+        public Message(String message) {
+            this.message = message;
+        }
 
-    	public String getMessage() {
-    		return message;
-    	}
+        public String getMessage() {
+            return message;
+        }
 
     }
-    
+
 }