在Java中实现在线留意(关注)、点赞、获取在线人数等功能,通常需要结合Web框架(如Spring Boot或Java Servlets)和数据库(如MySQL)来实现。下面是一个基于Spring Boot的简要示例:
- 用户在线状态管理:
- 为了获取在线人数,可以使用HttpSessionListener监听会话创建与销毁事件,维护一个在线用户计数器。
- 创建一个
OnlineUserCounter
类实现HttpSessionListener接口:
public class OnlineUserCounter implements HttpSessionListener {
private static AtomicInteger onlineCount = new AtomicInteger(0);
@Override
public void sessionCreated(HttpSessionEvent se) {
onlineCount.incrementAndGet();
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
onlineCount.decrementAndGet();
}
public int getOnlineCount() {
return onlineCount.get();
}
}
- 在Spring Boot的配置文件中注册该监听器。
- 关注功能:
- 定义一个
Follow
实体类表示关注关系,并通过Repository操作数据库记录关注状态。
- 定义一个
@Entity
@Table(name = "followers")
public class Follow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private User follower; // 关注者
@ManyToOne
private User following; // 被关注者
// 构造函数、getter和setter...
}
@Repository
public interface FollowRepository extends JpaRepository<Follow, Long> {
boolean existsByFollowerAndFollowing(User follower, User following);
// 其他查询方法...
}
- 点赞功能:
- 定义一个
Like
实体类表示点赞行为,并通过Repository操作数据库记录点赞信息。
- 定义一个
@Entity
@Table(name = "likes")
public class Like {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private User liker; // 点赞者
@ManyToOne
private Post post; // 被点赞的内容(这里以帖子为例)
// 构造函数、getter和setter...
}
@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {
long countByPost(Post post); // 获取某内容的点赞数量
// 其他查询方法...
}
- 服务层处理业务逻辑:
- 创建服务类处理用户的关注、点赞操作以及获取在线人数。
@Service
public class UserService {
@Autowired
private FollowRepository followRepository;
@Autowired
private LikeRepository likeRepository;
@Autowired
private OnlineUserCounter onlineUserCounter;
public void follow(User follower, User following) {
if (!followRepository.existsByFollowerAndFollowing(follower, following)) {
Follow follow = new Follow(follower, following);
followRepository.save(follow);
}
}
public void like(User liker, Post post) {
Like like = new Like(liker, post);
likeRepository.save(like);
}
public int getOnlineCount() {
return onlineUserCounter.getOnlineCount();
}
}
- 控制器处理HTTP请求:
- 创建控制器类处理前端发起的关注、点赞请求以及获取在线人数API调用。
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/follow/{followingId}")
public ResponseEntity<?> follow(@PathVariable("followingId") Long followingId, Principal principal) {
// 从Principal中获取当前登录用户信息
User follower = ...; // 根据principal解析出用户
User following = userRepository.findById(followingId).orElseThrow(() -> new ResourceNotFoundException("User not found"));
userService.follow(follower, following);
return ResponseEntity.ok().build();
}
@PostMapping("/like/post/{postId}")
public ResponseEntity<?> likePost(@PathVariable("postId") Long postId, Principal principal) {
// 同样获取当前登录用户
User liker = ...;
Post post = postRepository.findById(postId).orElseThrow(() -> new ResourceNotFoundException("Post not found"));
userService.like(liker, post);
return ResponseEntity.ok().build();
}
@GetMapping("/online-count")
public ResponseEntity<Integer> getOnlineCount() {
return ResponseEntity.ok(userService.getOnlineCount());
}
}
请注意,上述代码仅为简化版示例,实际应用中还需要考虑事务处理、权限验证等问题。同时,对于高并发场景下的点赞计数,推荐使用无锁数据结构如AtomicLong
或者数据库提供的原子更新操作等方式进行优化。