Springboot 用session监听器统计在线用户数量,netty框架工作原理

  • @Author : JCccc

  • @CreateTime : 2018-11-15

  • @Description :

  • @Point: Keep a good mood

**/

@WebListener

public class SessionListener implements HttpSessionListener{

private int onlineCount = 0;//记录session的数量

/**

  • session创建后执行

*/

@Override

public void sessionCreated(HttpSessionEvent se) {

onlineCount++;

System.out.println(“【HttpSessionListener监听器】 sessionCreated, onlineCount:” + onlineCount);

//将最新的onlineCount值存起来

se.getSession().getServletContext().setAttribute(“onlineCount”, onlineCount);

}

/**

  • session失效后执行

*/

@Override

public void sessionDestroyed(HttpSessionEvent se) {

if (onlineCount > 0) {

onlineCount–;

}

System.out.println(“【HttpSessionListener监听器】 sessionDestroyed, onlineCount:” + onlineCount);

//将最新的onlineCount值存起来

se.getSession().getServletContext().setAttribute(“onlineCount”, onlineCount);

}

}

第二步. 好了其实已经完成了。

接下来就是单纯的校验:

首先模拟一个系统的登录接口,

@GetMapping(“/login”)

public String login(HttpSession session){

//模拟一个用户调用了登录接口,进入系统

return “用户登录”;

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3907)]

[外链图片转存中…(img-KtI8PNH4-1711787573908)]

[外链图片转存中…(img-ZKHrb4iq-1711787573908)]

[外链图片转存中…(img-LuwFF8n0-1711787573909)]

  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Spring Boot项目时,你可以使用Redis作为缓存或数据存储的解决方案。以下是一个示例,展示了如何在Spring Boot项目中使用Redis集合存储数据: 1. 首先,确保你的Spring Boot项目中已经添加了Redis依赖。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties或application.yml文件中配置Redis连接信息。例如: ```yaml spring.redis.host=localhost spring.redis.port=6379 ``` 3. 创建一个Redis配置类,用于配置Redis连接和操作。在该类上使用`@Configuration`注解,并添加`@EnableCaching`注解启用缓存。 ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 4. 创建一个服务类,用于操作Redis集合存储数据。在该类中注入RedisTemplate,并使用`opsForSet()`方法获取集合操作对象。 ```java import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Set; @Service public class RedisSetService { @Resource private RedisTemplate<String, Object> redisTemplate; public void addToSet(String key, Object value) { redisTemplate.opsForSet().add(key, value); } public Set<Object> getSet(String key) { return redisTemplate.opsForSet().members(key); } public void removeFromSet(String key, Object value) { redisTemplate.opsForSet().remove(key, value); } } ``` 这样,你就可以在其他组件或服务中使用`RedisSetService`类来操作Redis集合数据了。例如: ```java @Service public class ExampleService { @Resource private RedisSetService redisSetService; public void example() { String setKey = "mySet"; redisSetService.addToSet(setKey, "value1"); redisSetService.addToSet(setKey, "value2"); Set<Object> setValues = redisSetService.getSet(setKey); System.out.println("Set values: " + setValues); redisSetService.removeFromSet(setKey, "value1"); setValues = redisSetService.getSet(setKey); System.out.println("Updated set values: " + setValues); } } ``` 这是一个简单的示例,展示了如何在Spring Boot项目中使用Redis集合存储数据。你可以根据自己的需求进行相应的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值