Git Product home page Git Product logo

Comments (10)

xiao-le-ge avatar xiao-le-ge commented on May 27, 2024 1
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor") 然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

按照您的写法试了下,依然没有回调 image

看了下源码,job为异步后,jobThread很快就执行完成execute方法,之后会立马去获取jobContext,获取不到你设置的handlMsg
image

from xxl-job.

xiao-le-ge avatar xiao-le-ge commented on May 27, 2024

你需要将你job的context传递到你的异步线程里去

from xxl-job.

Hemione521 avatar Hemione521 commented on May 27, 2024

你需要将你job的context传递到你的异步线程里去

可以详细说一下吗?

from xxl-job.

xiao-le-ge avatar xiao-le-ge commented on May 27, 2024

你需要将你job的context传递到你的异步线程里去

可以详细说一下吗?

//如果你@async指定了线程池,你需要在实例化线程池的时候这么操作
//假如你用的是ThreadPoolTaskExecutor

@Bean("async-executor")
    public ThreadPoolTaskExecutor asyncExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(30);
        executor.setQueueCapacity(256);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("async-");
        executor.setTaskDecorator(runnable -> {
            //获取主线程的上下文
            XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
            return () -> {
                //设置异步线程上下文
                XxlJobContext.setXxlJobContext(context);
                runnable.run();
            };
        });
        return executor;
    }

from xxl-job.

Hemione521 avatar Hemione521 commented on May 27, 2024

你需要将你job的context传递到你的异步线程里去

可以详细说一下吗?

//如果你@async指定了线程池,你需要在实例化线程池的时候这么操作 //假如你用的是ThreadPoolTaskExecutor

@Bean("async-executor")
    public ThreadPoolTaskExecutor asyncExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(30);
        executor.setQueueCapacity(256);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("async-");
        executor.setTaskDecorator(runnable -> {
            //获取主线程的上下文
            XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
            return () -> {
                //设置异步线程上下文
                XxlJobContext.setXxlJobContext(context);
                runnable.run();
            };
        });
        return executor;
    }

之前的代码和这个差不多,刚才按照您的写法试了下,还是不行。
代码如下:

@Configuration
public class TestAsyncConfig {
    @Bean("async-executor")
    public ThreadPoolTaskExecutor asyncExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(30);
        executor.setQueueCapacity(256);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("async-");
        executor.setTaskDecorator(runnable -> {
            //获取主线程的上下文
            XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext();
            return () -> {
                //设置异步线程上下文
                XxlJobContext.setXxlJobContext(xxlJobContext);
                runnable.run();
            };
        });
        return executor;
    }
}`
定时任务类如下:
`@Component
@RequiredArgsConstructor
@Async("async-executor")
public class TestSyncSchedule {

    @XxlJob(value = "testJob")
    public void testJob) {
        try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
    }

执行结果如下图:
image
最新一条是去掉异步线程池之后的效果,这个是期望效果。

from xxl-job.

xiao-le-ge avatar xiao-le-ge commented on May 27, 2024
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor")
然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

from xxl-job.

Hemione521 avatar Hemione521 commented on May 27, 2024
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor") 然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

按照您的写法试了下,依然没有回调
image

from xxl-job.

Hemione521 avatar Hemione521 commented on May 27, 2024
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor") 然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

按照您的写法试了下,依然没有回调 image

看了下源码,job为异步后,jobThread很快就执行完成execute方法,之后会立马去获取jobContext,获取不到你设置的handlMsg image

所以不应该用异步是吧

from xxl-job.

xiao-le-ge avatar xiao-le-ge commented on May 27, 2024
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor") 然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

按照您的写法试了下,依然没有回调 image

看了下源码,job为异步后,jobThread很快就执行完成execute方法,之后会立马去获取jobContext,获取不到你设置的handlMsg image

所以不应该用异步是吧

是的

from xxl-job.

Hemione521 avatar Hemione521 commented on May 27, 2024
@Async("async-executor")

可以试下将你的业务代码封装到单独的类和方法,然后在该方法上加@async("async-executor") 然后在testJob中调用该方法

@Service
public class TestService{
       @Async("async-executor")
       public void testService(){
           try {
            Thread.sleep(3000);
            XxlJobHelper.handleSuccess("job executed success");
        } catch (Exception e) {
            XxlJobHelper.handleFail("job executed fail");
        }
       }
}


@Component
public class TestSyncSchedule {

    @
    private  TestService service;

    @XxlJob(value = "testJob")
    public void testJob) {
        service.testService();
    }

按照您的写法试了下,依然没有回调 image

看了下源码,job为异步后,jobThread很快就执行完成execute方法,之后会立马去获取jobContext,获取不到你设置的handlMsg image

所以不应该用异步是吧

是的

了解了 感谢

from xxl-job.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.