-
Notifications
You must be signed in to change notification settings - Fork 90
/
SessionBinderJobListener.java
95 lines (84 loc) · 3.4 KB
/
SessionBinderJobListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.plugins.quartz.listeners;
import grails.core.GrailsApplication;
import grails.persistence.support.PersistenceContextInterceptor;
import grails.util.Holders;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;
/**
* JobListener implementation which wraps the execution of a Quartz Job in a
* persistence context, via the persistenceInterceptor.
*
* @author Sergey Nebolsin (nebolsin@gmail.com)
* @since 0.2
*/
public class SessionBinderJobListener extends JobListenerSupport {
private static final transient Log LOG = LogFactory.getLog(SessionBinderJobListener.class);
public static final String NAME = "sessionBinderListener";
private PersistenceContextInterceptor persistenceInterceptor;
public String getName() {
return NAME;
}
/**
* It is used by the Spring to inject a persistence interceptor.
* @return the reference of the currently active bean implementation of persistenceInterceptor
*/
@SuppressWarnings("UnusedDeclaration")
public PersistenceContextInterceptor getPersistenceInterceptor() {
return persistenceInterceptor;
}
/**
* It is used by the Spring to inject a persistence interceptor.
* @param persistenceInterceptor - Normally applied by bean injection to set the reference to the persistenceInterceptor
*/
@SuppressWarnings("UnusedDeclaration")
public void setPersistenceInterceptor(PersistenceContextInterceptor persistenceInterceptor) {
this.persistenceInterceptor = persistenceInterceptor;
}
/**
* Before job executing. Init persistence context.
*/
public void jobToBeExecuted(JobExecutionContext context) {
if (persistenceInterceptor != null) {
persistenceInterceptor.init();
LOG.debug("Persistence session is opened.");
}
}
/**
* After job executing. Flush and destroy persistence context.
*/
public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) {
if (persistenceInterceptor != null) {
try {
persistenceInterceptor.flush();
persistenceInterceptor.clear();
LOG.debug("Persistence session is flushed.");
} catch (Exception e) {
LOG.error("Failed to flush session after job: " + context.getJobDetail().getDescription(), e);
} finally {
try {
persistenceInterceptor.destroy();
} catch (Exception e) {
LOG.error("Failed to finalize session after job: " + context.getJobDetail().getDescription(), e);
}
}
}
}
}