发布动态
一、MovementsController
| package com.tanhua.server.controller; |
| |
| import com.tanhua.model.mongo.Movement; |
| import com.tanhua.server.service.MovementsService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.PostMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RestController; |
| import org.springframework.web.multipart.MultipartFile; |
| |
| import java.io.IOException; |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/movements") |
| public class MovementsController { |
| |
| |
| |
| @Autowired |
| private MovementsService movementsService; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @PostMapping |
| public ResponseEntity publishMoments(Movement movement, MultipartFile[] imageContent) throws IOException { |
| |
| |
| movementsService.publishMoments(movement,imageContent); |
| |
| |
| return ResponseEntity.ok(null); |
| } |
| |
| |
| |
| } |
| |
二、MovementsService
| package com.tanhua.server.service; |
| |
| import com.tanhua.autoconfig.template.OssTemplate; |
| import com.tanhua.dubbo.api.MovementsApi; |
| import com.tanhua.model.mongo.Movement; |
| import com.tanhua.model.vo.ErrorResult; |
| import com.tanhua.server.exception.BusinessException; |
| import com.tanhua.server.interceptor.ThreadLocalUntils; |
| import org.apache.dubbo.config.annotation.DubboReference; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| import org.springframework.util.StringUtils; |
| import org.springframework.web.multipart.MultipartFile; |
| |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| @Service |
| public class MovementsService { |
| |
| |
| @DubboReference |
| private MovementsApi movementsApi; |
| |
| @Autowired |
| private OssTemplate ossTemplate; |
| |
| |
| public void publishMoments(Movement movement, MultipartFile[] imageContent) throws IOException { |
| |
| |
| if(StringUtils.isEmpty(movement.getTextContent())){ |
| throw new BusinessException(ErrorResult.contentError()); |
| } |
| |
| |
| Long userId = ThreadLocalUntils.getUserId(); |
| |
| |
| |
| List<String> urls = new ArrayList<>(); |
| for (MultipartFile multipartFile : imageContent) { |
| String imageUrl = ossTemplate.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream()); |
| urls.add(imageUrl); |
| } |
| |
| |
| movement.setUserId(userId); |
| movement.setMedias(urls); |
| |
| |
| movementsApi.publishMoments(movement); |
| |
| } |
| } |
| |
三、MovementsApiImpl
| package com.tanhua.dubbo.api; |
| |
| import com.tanhua.dubbo.utils.IdWorker; |
| import com.tanhua.model.mongo.Friend; |
| import com.tanhua.model.mongo.Movement; |
| import com.tanhua.model.mongo.MovementTimeLine; |
| import org.apache.dubbo.config.annotation.DubboService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.data.mongodb.core.MongoTemplate; |
| import org.springframework.data.mongodb.core.query.Criteria; |
| import org.springframework.data.mongodb.core.query.Query; |
| |
| import java.util.List; |
| |
| @DubboService |
| public class MovementsApiImpl implements MovementsApi { |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| |
| @Autowired |
| private IdWorker idWorker; |
| |
| |
| |
| public void publishMoments(Movement movement) { |
| |
| |
| |
| movement.setPid(idWorker.getNextId("movement")); |
| movement.setCreated(System.currentTimeMillis()); |
| mongoTemplate.save(movement); |
| |
| |
| Criteria criteria = Criteria.where("userId").is(movement.getUserId()); |
| Query query = Query.query(criteria); |
| List<Friend> friends = mongoTemplate.find(query, Friend.class); |
| |
| |
| for (Friend friend : friends) { |
| MovementTimeLine mtl = new MovementTimeLine(); |
| mtl.setMovementId(movement.getId()); |
| mtl.setUserId(friend.getUserId()); |
| mtl.setFriendId(friend.getFriendId()); |
| mtl.setCreated(System.currentTimeMillis()); |
| } |
| |
| |
| } |
| } |
| |
四、代码优化
1.启动类上添加@@EnableAsync注解
2.新建一个类,异步处理工具类,SaveTimeLIne;
把它注入为spring管理的bean,在方法上使用@Async注解,把查询好友跟添加好友时间线的代码抽取出来
| package com.tanhua.dubbo.utils; |
| |
| import com.tanhua.model.mongo.Friend; |
| import com.tanhua.model.mongo.MovementTimeLine; |
| import org.bson.types.ObjectId; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.data.mongodb.core.MongoTemplate; |
| import org.springframework.data.mongodb.core.query.Criteria; |
| import org.springframework.data.mongodb.core.query.Query; |
| import org.springframework.scheduling.annotation.Async; |
| import org.springframework.stereotype.Component; |
| |
| import java.util.List; |
| |
| @Component |
| public class SaveTimeLIne { |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| |
| @Async |
| public void saveTimeLine(Long userId, ObjectId movementId){ |
| |
| |
| |
| Criteria criteria = Criteria.where("userId").is(userId); |
| Query query = Query.query(criteria); |
| List<Friend> friends = mongoTemplate.find(query, Friend.class); |
| |
| |
| for (Friend friend : friends) { |
| MovementTimeLine mtl = new MovementTimeLine(); |
| mtl.setMovementId(movementId); |
| mtl.setUserId(friend.getUserId()); |
| mtl.setFriendId(friend.getFriendId()); |
| mtl.setCreated(System.currentTimeMillis()); |
| } |
| |
| } |
| } |
| |
三、api实现类上直接调用方法
| package com.tanhua.dubbo.api; |
| |
| import com.tanhua.dubbo.utils.IdWorker; |
| import com.tanhua.dubbo.utils.SaveTimeLIne; |
| import com.tanhua.model.mongo.Movement; |
| import org.apache.dubbo.config.annotation.DubboService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.data.mongodb.core.MongoTemplate; |
| |
| @DubboService |
| public class MovementsApiImpl implements MovementsApi { |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| |
| @Autowired |
| private IdWorker idWorker; |
| |
| |
| |
| @Autowired |
| private SaveTimeLIne saveTimeLIne; |
| |
| |
| |
| |
| |
| public void publishMoments(Movement movement) { |
| |
| |
| |
| movement.setPid(idWorker.getNextId("movement")); |
| movement.setCreated(System.currentTimeMillis()); |
| mongoTemplate.save(movement); |
| |
| saveTimeLIne.saveTimeLine(movement.getUserId(), movement.getId()); |
| |
| } |
| } |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏