亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Vertx session 使用須知

標簽:
Java

要使用vertx的session,必须把sessionHandler设为router的第一个匹配的route,否则将报错。比如下面的代码将引起报错:

public class LeanS1 extends AbstractVerticle {	  @Override	  public void start() throws Exception {	    Router router = Router.router(vertx);	    router.route("/test").handler(this::test3);	    router.route().handler(CookieHandler.create());	    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));	    //......	  }	

在运行的时候,浏览器输入:localhost:8080/test ,就会报错:

严重: Unexpected exception in routejava.lang.NullPointerException	at io.robin0909.ann.init.LeanS1.test3(LeanS1.java:47)	at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:219)	at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:120)	at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:120)	at io.vertx.ext.web.impl.RouterImpl.accept(RouterImpl.java:79)	at io.vertx.core.http.impl.Http1xServerConnection.processMessage(Http1xServerConnection.java:433)	at io.vertx.core.http.impl.Http1xServerConnection.handleMessage(Http1xServerConnection.java:141)	at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:683)	at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:636)	at io.vertx.core.net.impl.VertxHandler.lambda$channelRead$1(VertxHandler.java:146)	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:337)	at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:195)	at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:144)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)	at io.vertx.core.http.impl.HttpServerImpl$Http2UpgradeHandler.channelRead(HttpServerImpl.java:952)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310)	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)	at io.vertx.core.http.impl.Http1xOrH2CHandler.end(Http1xOrH2CHandler.java:60)	at io.vertx.core.http.impl.Http1xOrH2CHandler.channelRead(Http1xOrH2CHandler.java:38)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935)	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:141)	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)	at java.lang.Thread.run(Unknown Source)

当把sessionHandler设为router的第一个匹配的route时,运行正常,正确代码如下:

public void start() throws Exception {	    Router router = Router.router(vertx);	    	    router.route().handler(CookieHandler.create());	    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));	    router.route("/test").handler(this::test3);	    //.....



对于router.route().handler(...) 所指定的通用路径处理函数,无论指定了多少个通用路径函数,都会在访问时被顺序执行一遍。但是对于router.route("/test").handler(...) 所指定的专用路径处理函数,如果指定了多个,那么不会自动全部执行,只执行第一个。只有遇到routingContext.next();指令的时候才会继续执行下一个处理函数


 这点可以通过代码测试。

比如:

public void start() throws Exception {	    Router router = Router.router(vertx);	    	    router.route().handler(CookieHandler.create());	    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));	    router.route("/test").handler(this::test3);	    router.route("/test").handler(this::test4);	    router.route("/test").handler(this::test5);	    	    //.....

对于上述代码,当在浏览器输入localhost:8080/test的时候, test3的结尾如果没有next指令,test4,test5就不会被执行。


vertx 服务在部分安卓手机上页面跳转的时候,常常丢失session,后来发现是Cookie的Path问题导致出错,所以,可以在verticle文件中加入如下代码:

//to make session work correctly on Huawei & Mi phone, set Cookie Path to root '/'         Set<Cookie> ccList=routingContext.cookies();        for(Cookie ck:ccList){        	ck.setPath("/");        	        }


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消