Skip to content

Instantly share code, notes, and snippets.

@qlong8807
qlong8807 / Task.java
Created March 6, 2019 09:15
springboot Scheduled 多任务 task
//1. 开启功能@EnableScheduling
//2. 配置线程池
//spring默认(不配置线程池)为单线程,一个任务执行完成才会执行下一个任务。
@Configuration
public class TaskConfigure{
@Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler=new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);//线程池
return taskScheduler;
@qlong8807
qlong8807 / RedisHelper.java
Created March 5, 2019 12:09
redis实现分布式锁。springboot下注入redisTemplate,redis的expire、delete等操作需要用redisTemplate而不能用opsForValue等。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Objects;
@Component("redisHelper")
public class RedisHelper {
@qlong8807
qlong8807 / startup.sh
Created January 30, 2019 09:35
启动当前目录下的jar结尾的Java程序 start|stop|status
#!/bin/bash
source /etc/profile
basedir=`dirname $0`
cd $basedir
for i in `ls -t *.jar`
do
#echo $i
break
done
@echo off
setlocal enabledelayedexpansion
set port=30056
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%port%"') do (
if "%%e%" == "" (
set pid=%%d
) else (
set pid=%%e
)
echo !pid!
sh内容:
source ~/.bash_profile
killall java
nohup java -jar /root/springbootdemo/springbootdemo-0.0.1-SNAPSHOT.jar > nohup.log 2>&1 &
执行命令
sh /root/sh/restart-springboot.sh
@qlong8807
qlong8807 / SpringContextUtil.java
Created December 19, 2018 10:01
java代码获取Spring容器中的bean
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
/*
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//1当普通调用时,一般跳转到自定义的错误页面。
//2当ajax调用时,可返回约定的数据对象,方便页面统一处理。
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@qlong8807
qlong8807 / spring下载文件名乱码.java
Created December 4, 2018 03:13
spring下载文件,解决中文文件名乱码问题
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
String realFileName = URLEncoder.encode(realFileName, StandardCharsets.UTF_8.toString());
// 解决中文文件名乱码关键行
response.setHeader("Content-Disposition", "attachment; filename=\"" + realFileName + "\"; filename*=utf-8''" + realFileName);
Path path = Paths.get(filePath+filename);
Files.copy(path, response.getOutputStream());
response.flushBuffer();
@qlong8807
qlong8807 / java启动.sh
Created November 22, 2018 07:43
一个启动Java程序的SH脚本
#!/usr/bin/env bash
if [ ! -n "${LIQUIBASE_HOME+x}" ]; then
# echo "LIQUIBASE_HOME is not set."
## resolve links - $0 may be a symlink
PRG="$0"
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`