Skip to content

Commit

Permalink
[#3]
Browse files Browse the repository at this point in the history
initial commit for email notification after job
  • Loading branch information
scalding committed Sep 16, 2013
1 parent 677457d commit da51422
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ dependencies {
compile "org.springframework:spring-${it}:${project.springVersion}"
}

compile "javax.mail:mail:1.4.4"

// BoneCP jdbc connection pool
compile "com.jolbox:bonecp:0.7.1.RELEASE"

Expand Down
10 changes: 10 additions & 0 deletions etc/openregistry/config/config.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
configuration {
id "HR"
notification {
email {
host ""
port 465
username ""
password ""
from ""
to ""
}
}
person {
keyField "empl_id"
names {
Expand Down
41 changes: 40 additions & 1 deletion src/main/groovy/org/openregistry/loader/CleanupListener.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,49 @@ package org.openregistry.loader

import org.springframework.batch.core.JobExecution
import org.springframework.batch.core.annotation.AfterJob
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.mail.javamail.JavaMailSenderImpl
import org.springframework.mail.javamail.MimeMessageHelper

import javax.annotation.Resource

class CleanupListener {
@Resource(name = "sorConfiguration")
Map<String, SorConfiguration> sorConfigurationMap

@AfterJob
void cleanup(JobExecution jobExecution) {
// do stuff
SorConfiguration sorConfiguration = sorConfigurationMap[jobExecution.jobParameters.getString("sor")]

if (sorConfiguration.notificationConfiguration.emailConfiguration) {
EmailNotificationConfiguration emailNotificationConfiguration = sorConfiguration.notificationConfiguration.emailConfiguration
JavaMailSender mailSender = new JavaMailSenderImpl().with {
host = emailNotificationConfiguration.host
port = emailNotificationConfiguration.port
protocol = "smtps"
username = emailNotificationConfiguration.username
password = emailNotificationConfiguration.password
javaMailProperties = new Properties().with {
setProperty("mail.smtps.auth", true)
setProperty("mail.smtps.starttls.enable", true)
setProperty("mail.smtps.debug", false)

return it
}
return it
}
def message = mailSender.createMimeMessage().with {
new MimeMessageHelper(it, true).with {
setFrom(emailNotificationConfiguration.from)
addBcc(emailNotificationConfiguration.to)
subject = "OpenRegistry Loader Job"
text = """
The job completed at ${new Date()}
"""
}
return it
}
mailSender.send(message)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DatabaseViewSorConfigurationFactoryBean implements FactoryBean<Map<String,
// TODO: move

def field = { d, name, arg ->
if (arg instanceof String) {
if (arg instanceof String || arg instanceof Integer) {
d."${name}" = arg
} else if (arg instanceof Closure) {
d."${name}" = new FieldConfiguration()
Expand Down Expand Up @@ -47,7 +47,10 @@ class DatabaseViewSorConfigurationFactoryBean implements FactoryBean<Map<String,
}

DatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(
["person": PersonDatabaseViewSorConfiguration]
[
"person": PersonDatabaseViewSorConfiguration,
"notification": NotificationConfiguration
]
)
PersonDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(
[
Expand All @@ -66,6 +69,11 @@ class DatabaseViewSorConfigurationFactoryBean implements FactoryBean<Map<String,
'phones': PhonesDatabaseViewSorConfiguration
]
)
NotificationConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(
[
'email': EmailNotificationConfiguration
]
)

NamesDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)
EmailAddressesDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)
Expand All @@ -75,6 +83,7 @@ class DatabaseViewSorConfigurationFactoryBean implements FactoryBean<Map<String,
AddressesDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)
LeavesDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)
PhonesDatabaseViewSorConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)
EmailNotificationConfiguration.metaClass.methodMissing = notSoSimpleMethodMissing(null)

FieldConfiguration.metaClass.methodMissing = { name, args ->
if (name in ['value', 'staticValue', 'normalizer']) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/groovy/org/openregistry/loader/SorConfiguration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ class SorConfiguration {
String description

def personConfiguration
def notificationConfiguration
}

class NotificationConfiguration {
def emailConfiguration
}

class EmailNotificationConfiguration {
def host
def port
def username
def password
def from
def to
}

class SorPersonConfiguration {
Expand Down
11 changes: 9 additions & 2 deletions src/main/resources/jobs/sor-load-job.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</bean>

<bean id="systemOfRecordRepository" class="org.openregistry.loader.DSLSystemOfRecordRepository">
<property name="databaseViewSorConfigurations" ref="sorConfiguration" />
<property name="databaseViewSorConfigurations" ref="sorConfiguration"/>
</bean>

<import resource="classpath:jobs/launcher/jobs-launcher-context.xml"/>
Expand Down Expand Up @@ -56,10 +56,17 @@
</property>
</bean>

<bean id="cleanupListener"
class="org.openregistry.loader.CleanupListener"
scope="step">
<property name="sorConfiguration" value="#{sorConfiguration[jobParameters['sor']]}"/>
</bean>

<batch:job id="sorLoadJob" job-repository="jobRepository" incrementer="jobParametersIncrementer">
<batch:listeners>
<batch:listener>
<bean class="org.openregistry.loader.CleanupListener"/>
<bean id="cleanupListener"
class="org.openregistry.loader.CleanupListener"/>
</batch:listener>
</batch:listeners>
<batch:validator ref="jobParameterValidator"/>
Expand Down

0 comments on commit da51422

Please sign in to comment.