package com.alatus.mall.product.web;

import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redisson;
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities = categoryService.getLevelCategories(1);
//        视图解析器会自动拼串,不需要写具体位置和html后缀
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<SubCatalogVo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
//        获取一把锁,只要名字一样,就必定是同一把锁,底层用的原理还是redis
        RLock rLock = redisson.getLock("rLock");
//        加锁
//        加的锁会自带阻塞式等待,默认锁的时间是30s
//        锁会自动续期,如果业务超长,会自动给锁续上30s,不用像我们手动加锁一样,担心锁被自动删除
//        只要业务完成,就不会续期,即使不手动解锁,也会在30s后删除
        rLock.lock();
        try{
            System.out.println("加锁成功执行业务");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        finally {
//            解锁,假设业务代码出现问题或是没有执行,redisson也不会出现死锁
            rLock.unlock();
        }
        return "Hello";
    }
}
package com.alatus.mall.product.web;

import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redisson;
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities = categoryService.getLevelCategories(1);
//        视图解析器会自动拼串,不需要写具体位置和html后缀
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<SubCatalogVo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
//        获取一把锁,只要名字一样,就必定是同一把锁,底层用的原理还是redis
        RLock rLock = redisson.getLock("rLock");
//        加锁
//        加的锁会自带阻塞式等待,默认锁的时间是30s
//        锁会自动续期,如果业务超长,会自动给锁续上30s,不用像我们手动加锁一样,担心锁被自动删除
//        只要业务完成,就不会续期,即使不手动解锁,也会在30s后删除
        rLock.lock();
        try{
            System.out.println("加锁成功执行业务");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        finally {
//            解锁,假设业务代码出现问题或是没有执行,redisson也不会出现死锁
            rLock.unlock();
        }
        return "Hello";
    }
}
Logo

更多推荐