Vertx session 使用须知

news/2024/7/5 2:26:59

要使用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 route
java.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("/");        	
        }



http://www.niftyadmin.cn/n/1998590.html

相关文章

手机的缘分

发信人: huoma (火马), 信区: Heartsong标 题: 手机的缘分发信站: BBS 水木清华站 (Wed May 2 08:57:07 2007), 站内 手机失窃。 2005年5月1日&#xff0c;诺基亚1100取代万事不灵的小灵通泰丰888&#xff0c;500米身价投戎洒家麾下。两年来&#xff0c;如影随形、任劳任怨…

计算机知识书面,中学生计算机知识书面考试模拟试卷.doc

中学生计算机知识书面考试模拟试卷中学生计算机知识书面考试模拟试题(1)试卷说明&#xff1a;本试卷考试时间为90分钟。试卷答案一律写在答卷纸上&#xff0c;按各试题答案编号填写在答卷上相应编号处。答题一律用钢笔或圆珠笔书写&#xff0c;不得用铅笔书写。4&#xff0e;凡…

银广夏事件

《财经》杂志8月号发表封面文章《银广夏陷阱》&#xff0c;揭露深圳股票交易所上市公司银广夏(0557)1999、2000年度业绩绝大部分来自造假。  银广夏是近两年来的大牛股&#xff0c;从股价到业绩&#xff0c;均创下了令人眩目的记录&#xff1a;  1999年&#xff0c;银广夏的…

html 传参阻止冒泡,angular阻止冒泡事件

当元素多层嵌套的时候&#xff0c;每层都有点击事件&#xff0c;它就会发生冒泡&#xff0c;一层一层的触发&#xff0c;但有时候我们只想触发某一层&#xff0c;不想让其他层的事件触发&#xff0c;这就需要阻止冒泡事件了。以angular项目为例&#xff0c;我们都知道angularJS…

常见程序题——算法

1、求出一个字符在某一个字符串中出现的位置和次数。 /// <summary> /// /// </summary> /// <param name"str">被查找的字符串</param> /// <param name"strKey">查找的字符</param> p…

计算机office基础知识题库,计算机一级MS Office基础考试题库

信息安全的内涵在不断地延伸&#xff0c;从最初的信息保密性发展到信息的完整性、可用性、可控性和不可否认性。下面是小编整理的关于计算机一级MS Office基础考试题库&#xff0c;希望大家认真阅读!单选题1). 最早的应用领域是A.信息处理B.科学计算C.过程控制D.人工智能正确答…

安然事件

从去年底美国能源大公司安然公司申请破产保护以来&#xff0c;美国华尔街似乎就一直没有太平过。上市公司欺诈的阴影挥之不去&#xff0c;此起彼伏&#xff0c;直至6月25日&#xff0c;在美国长途电话市场位居第二的世界通信公司又爆出了38亿美元的财务欺诈案&#xff0c;紧接着…

《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris

题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3;/*** Description:遍历二叉树的神级方法 morris* Author: lizhouwei* CreateDate: 2018/4/14 17:15* Modify by:* ModifyDate:*/ public class Chapter3_5 {//morris中序public void morrisInOrder(…