1

I want to call an Oracle PL/SQL procedure from Spring. The procedure takes a lot of time so I would like it to be executed in the background (asynchronously) and briefly return from the DAO method and controller. What is the most elegant method to return from a long-running procedure? I do not have to track its execution in the request which calls the procedure.

What currently I have is:

@Override
public void submitJob(final Integer param1, final String param2, final Integer param3) {
    jdbcTemplate.execute(
        new CallableStatementCreator() {
            public CallableStatement createCallableStatement(Connection con) throws SQLException{
                CallableStatement cs = con.prepareCall("{call PCKG.MY_PROC(?, NULL, ?, ?)}");
                cs.setInt(1, param1);
                cs.setString(2, param2);
                cs.setInt(3, param3);
                return cs;
            }
        },
        new CallableStatementCallback() {
            public Object doInCallableStatement(CallableStatement cs) throws SQLException{
                cs.execute();
                return null;
            }
        }
    );
}
wsk
  • 65
  • 2
  • 7
  • Put `@Async` on the `submitJob` method, register a `TaskExecutor` together with `` and you should be good to go. Check http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async – M. Deinum May 14 '14 at 14:23

1 Answers1

1

You can add @Async on the submitJob method:

@Async
@Override
public void submitJob(final Integer param1, final String param2, final Integer param3) {
  jdbcTemplate.execute( ...

and the following to your Application context:

    <task:annotation-driven executor="asynExecutor"/>   
    <task:executor id="asynExecutor" pool-size="5" />

Check other similar questions:

Community
  • 1
  • 1
J. Chomel
  • 8,193
  • 15
  • 41
  • 69