spring task extension

spring task extension project

Лицензия

Лицензия

Группа

Группа

com.github.jim2ha0
Идентификатор

Идентификатор

spring-task-support
Последняя версия

Последняя версия

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

spring task extension
spring task extension project
Система контроля версий

Система контроля версий

https://github.com/jim2Ha0/spring-task-extension

Скачать spring-task-support

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.github.jim2ha0/spring-task-support/ -->
<dependency>
    <groupId>com.github.jim2ha0</groupId>
    <artifactId>spring-task-support</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.jim2ha0/spring-task-support/
implementation 'com.github.jim2ha0:spring-task-support:1.0.0'
// https://jarcasting.com/artifacts/com.github.jim2ha0/spring-task-support/
implementation ("com.github.jim2ha0:spring-task-support:1.0.0")
'com.github.jim2ha0:spring-task-support:jar:1.0.0'
<dependency org="com.github.jim2ha0" name="spring-task-support" rev="1.0.0">
  <artifact name="spring-task-support" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.jim2ha0', module='spring-task-support', version='1.0.0')
)
libraryDependencies += "com.github.jim2ha0" % "spring-task-support" % "1.0.0"
[com.github.jim2ha0/spring-task-support "1.0.0"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

spring-task-extension

1.Include Dependency

gradle style
dependencies{
    compile "com.github.jim2ha0:spring-task-core:1.0.0"
    compile "com.github.jim2ha0:spring-task-support:1.0.0"
}

2.implement Lock interface(custom define)

FileLock(implements Lock interface) sample in project "spring-task-support"
package com.github.jim2ha0;

import com.github.jim2ha0.lock.Lock;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

public class FileLock implements Lock<java.nio.channels.FileLock> {
    @Override
    public java.nio.channels.FileLock lock(String key) {
        try{
            FileOutputStream fileOutputStream = new FileOutputStream(key);
            FileChannel fileChannel = fileOutputStream.getChannel();
            return fileChannel.lock(0L,10000L,Boolean.FALSE);
        }catch (OverlappingFileLockException| IOException e){
            Path lf = Paths.get(key);
            if(Files.notExists(lf)){
                try {
                    Files.createDirectories(lf.getParent());
                    Files.createFile(lf);
                } catch (IOException e1) {
                    //e1.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public java.nio.channels.FileLock tryLock(String key) {
        try{
            FileOutputStream fileOutputStream = new FileOutputStream(key);
            FileChannel fileChannel = fileOutputStream.getChannel();
            return fileChannel.tryLock(0L,10000L,Boolean.FALSE);
        }catch (OverlappingFileLockException| IOException e){
            Path lf = Paths.get(key);
            if(Files.notExists(lf)){
                try {
                    Files.createDirectories(lf.getParent());
                    Files.createFile(lf);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public void releaseLock(java.nio.channels.FileLock resource) {
        if(Objects.nonNull(resource)){
            try {
                resource.release();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }
}


3.use @Scheduled annotation with Lock implements

package com.github.jim2ha0.service;

import com.github.jim2ha0.FileLock;
import com.github.jim2ha0.annotation.Scheduled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.LockSupport;

@Service
public class ScheduledTaskService {

    private Logger logger = LoggerFactory.getLogger(ScheduledTaskService.class);
    @Scheduled(lockClazz = FileLock.class,lockName = "\\a\\b\\c.lock",cron = "0/5 * * * * ?")
    public void testScheduledTask1(){
        logger.info("=====testScheduledTask1==========");
        LockSupport.parkNanos(1_000_000_000L);
    }

    @Scheduled(lockClazz = FileLock.class,lockName = "\\a\\b\\d.lock",cron = "0/5 * * * * ?")
    public void testScheduledTask2(){
        logger.info("=====testScheduledTask2==========");
        LockSupport.parkNanos(1_000_000_000L);
    }
}

4. enable by using @com.github.jim2ha0.annotation.EnableScheduling annotation

@com.github.jim2ha0.annotation.EnableScheduling
@SpringBootApplication
public class Application  extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

Версии библиотеки

Версия
1.0.0