瀏覽代碼

临时提交

huangzhiyong 6 年之前
父節點
當前提交
4ab5e5028c

+ 28 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/cache/Receiver.java

@ -0,0 +1,28 @@
package com.yihu.jw.healthyhouse.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.CountDownLatch;
/**
 * @author HZY
 * @created 2018/10/8 15:05
 */
public class Receiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
    private CountDownLatch latch;
    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }
    public void receiveMessage(String message) {
        LOGGER.info("Received <" + message + ">");
        latch.countDown();
    }
    }

+ 68 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/config/RedisConfig.java

@ -0,0 +1,68 @@
package com.yihu.jw.healthyhouse.config;
import com.yihu.jw.healthyhouse.cache.Receiver;
import org.springframework.boot.SpringApplication;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
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.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import java.util.concurrent.CountDownLatch;
/**
 * @author HZY
 * @created 2018/10/8 15:12
 */
@Configuration
public class RedisConfig  {
    /*
     * Redis消息监听器容器
     * 这个容器加载了RedisConnectionFactory和消息监听器
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage"));
        return container;
    }
    /*
     * 将Receiver注册为一个消息监听器,并指定消息接收的方法(receiveMessage)
     * 如果不指定消息接收的方法,消息监听器会默认的寻找Receiver中的handleMessage这个方法作为消息接收的方法
     */
    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver){
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    /*
     * Receiver实例
     */
    @Bean
    Receiver receiver(CountDownLatch latch){
        return new Receiver(latch);
    }
    @Bean
    CountDownLatch latch(){
        return new CountDownLatch(1);
    }
    /*
     * Redis Template 用来发送消息
     */
    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }
}

+ 1 - 1
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/controller/facilities/FacilitiesController.java

@ -302,7 +302,7 @@ public class FacilitiesController extends EnvelopRestEndpoint {
            }
        } catch (Exception e) {
            throw new ManageException("导入设施列表异常!", e);
            return failed("导入异常");
        }
        return failed("导入失败");
    }

+ 15 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/controller/user/UserController.java

@ -277,5 +277,20 @@ public class UserController  extends EnvelopRestEndpoint {
    }
    @PostMapping(value = "/heartbeat")
    @ApiOperation(value = "用户在线-心跳接口", notes = "心跳接口")
    public Envelop heartbeat(
            @ApiParam(name = "userId", value = "用户ID", defaultValue = "")
            @RequestParam(value = "userId") String userId) {
        try {
              userService.setUserActivated(userId, 60);
            return success("心跳正常");
        } catch (Exception e) {
            return failed("心跳异常");
        }
    }
}

+ 25 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/listener/MyKeyExpirationEventMessageListener.java

@ -0,0 +1,25 @@
package com.yihu.jw.healthyhouse.listener;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
 * @author HZY
 * @created 2018/10/8 15:11
 */
public class MyKeyExpirationEventMessageListener extends KeyExpirationEventMessageListener {
    public MyKeyExpirationEventMessageListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {
//      System.out.println(new String(message.getBody()));
//      System.out.println(new String(message.getChannel()));
//      System.out.println(new String(pattern));
//      super.onMessage(message, pattern);
    }
}

+ 3 - 3
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/service/user/LoginService.java

@ -66,7 +66,7 @@ public class LoginService  extends BaseJpaService {
            user.setUserType(LoginInfo.USER_TYPE_PATIENT);
        }
        if (HouseUserContant.activated_lock.equals(user)) {
        if (HouseUserContant.activated_lock.equals(user.getActivated())) {
            throw new ManageException("该用户已被冻结!");
        }
        //已注册用户更改用户状态
@ -274,7 +274,7 @@ public class LoginService  extends BaseJpaService {
        if (user == null) {
            throw new ManageException("该管理员账号不存在!");
        } else {
            if (HouseUserContant.activated_lock.equals(user)) {
            if (HouseUserContant.activated_lock.equals(user.getActivated())) {
                throw new ManageException("该用户已被冻结!");
            }
            //已注册用户更改用户状态
@ -308,7 +308,7 @@ public class LoginService  extends BaseJpaService {
            String message = "该管理员账号不存在!";
            throw new ManageException(message);
        } else {
            if (HouseUserContant.activated_lock.equals(user)) {
            if (HouseUserContant.activated_lock.equals(user.getActivated())) {
                throw new ManageException("该用户已被冻结!");
            }

+ 17 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/service/user/UserService.java

@ -24,6 +24,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springside.modules.persistence.DynamicSpecifications;
@ -33,6 +34,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
@ -41,11 +43,26 @@ import java.util.regex.Pattern;
 */
@Service
public class UserService extends BaseJpaService<User, UserDao> {
    private final String KEY_PREFIX = ":healthyHouse";
    private final String KEY_SUFFIX = ":activated";
    @Autowired
    private UserDao userDao;
    @Autowired
    private FacilityUsedRecordService facilityUsedRecordService;
    @Autowired
    private  RedisTemplate redisTemplate;
    /**
     * 设置用户在线状态
     * @param userId
     * @param expire
     */
    public void setUserActivated ( String userId, int expire) {
        String key =  KEY_PREFIX + userId + KEY_SUFFIX;
        redisTemplate.opsForValue().set(key, userId);
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }
    public User findById(String id) {
        return userDao.findById(id);