大数据下载防止系统崩溃解决方案一:阻塞集中式下载

 

/**

 * 下载控制器

 * 阻塞集中式下载 防止系统崩溃

 * 使用定时过期的缓存锁实现

 * 每10分钟只能下载一次

 **/

import com.google.common.cache.Cache;

import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;

public class DownloadControl {

 

    private static final String CACHE_KEY = "DOWNLOAD";

 

    /**

     * 下载权限保存地点

     *

     */

    private static final Cache<String, String> cache = CacheBuilder.newBuilder()

            .initialCapacity(1)

            .concurrencyLevel(1)

            .expireAfterWrite(10, TimeUnit.MINUTES)

            .build();

 

    /**

     * 尝试获取下载权限 未获取到权限

     */

    public static synchronized boolean notGetPermission() {

        return !tryGetPermission();

    }

 

    private static synchronized boolean tryGetPermission() {

        String hasLock = cache.getIfPresent(CACHE_KEY);

        if (hasLock == null) {

            cache.put(CACHE_KEY, "1");

            return true;

        }

        return false;

    }

    /**

     * 重置下载权限

     */

    public static synchronized void resetPermission() {

        cache.invalidate(CACHE_KEY);

    }

}

 

在下载方法中使用:

if(DownloadControl.notGetPermission()){

request.setAttribute(ReturnConstant.MEMO, "当前下载队列满!请稍后重试。");

return mapping.findForward(ReturnConstant.ERROR);

}

try{

下载代码

} catch (Exception e) {

String memo = "下载失败,请稍后再试 ···";

log.info("异常:{} ", memo, e);

}finally {

    DownloadControl.resetPermission();

}

 

测试:

public class DownloadControlTest {

    @Test

    public void hasPermission() throws InterruptedException {

        //模拟并发

        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {

            Thread.sleep(500);

            final int finalI = i;

            executorService.execute(new Runnable() {

                @Override

                public void run() {

                    try {

                        boolean res = DownloadControl.notGetPermission();

                        System.out.println(finalI + " " + res + "   " + DateUtil.getNow());

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

            });

        }

    }

 

    @Test

    public void resetPermission() throws InterruptedException {

        //模拟并发

        ExecutorService executorService = Executors.newCachedThreadPool();

        Thread.sleep(1000L);

        executorService.execute(new Runnable() {

            @Override

            public void run() {

                try {

                    boolean res = DownloadControl.notGetPermission();

                    System.out.println(res + "   " + DateUtil.getNow());

                } catch (Exception e) {

                    e.printStackTrace();

                }

                DownloadControl.resetPermission();

            }

        });

        Thread.sleep(1000L);

        executorService.execute(new Runnable() {

            @Override

            public void run() {

                try {

                    boolean res = DownloadControl.notGetPermission();

                    System.out.println(res + "   " + DateUtil.getNow());

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

        Thread.sleep(1000L);

        executorService.execute(new Runnable() {

            @Override

            public void run() {

                try {

                    boolean res = DownloadControl.notGetPermission();

                    System.out.println(res + "   " + DateUtil.getNow());

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值