Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchedulerFactoryBeanCustomizer now runs first so user customization is not overridden #3095

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Fixes

- SchedulerFactoryBeanCustomizer now runs first so user customization is not overridden ([#3095](https://github.com/getsentry/sentry-java/pull/3095))
- If you are setting global job listeners please also add `SentryJobListener`

## 7.1.0

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.quartz.JobExecutionContext
import org.quartz.JobExecutionException
import org.quartz.JobListener
import org.quartz.Scheduler
import org.quartz.core.QuartzScheduler
import org.slf4j.MDC
import org.springframework.aop.support.NameMatchMethodPointcut
import org.springframework.boot.autoconfigure.AutoConfigurations
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
import org.springframework.boot.context.annotation.UserConfigurations
Expand Down Expand Up @@ -784,6 +789,61 @@ class SentryAutoConfigurationTest {
}
}

@Test
fun `Sentry quartz job listener is added`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.enable-automatic-checkins=true")
.withUserConfiguration(QuartzAutoConfiguration::class.java)
.run {
val jobListeners = it.getBean(Scheduler::class.java).listenerManager.jobListeners
assertThat(jobListeners).hasSize(1)
assertThat(jobListeners[0]).matches(
{ it.name == "sentry-job-listener" },
"is sentry job listener"
)
}
}

@Test
fun `user defined SchedulerFactoryBeanCustomizer overrides Sentry Customizer`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.enable-automatic-checkins=true")
.withUserConfiguration(QuartzAutoConfiguration::class.java, CustomSchedulerFactoryBeanCustomizerConfiguration::class.java)
.run {
val jobListeners = it.getBean(Scheduler::class.java).listenerManager.jobListeners
assertThat(jobListeners).hasSize(1)
assertThat(jobListeners[0]).matches(
{ it.name == "custom-job-listener" },
"is custom job listener"
)
}
}

@Configuration(proxyBeanMethods = false)
open class CustomSchedulerFactoryBeanCustomizerConfiguration {
class MyJobListener : JobListener {
override fun getName() = "custom-job-listener"

override fun jobToBeExecuted(context: JobExecutionContext?) {
// do nothing
}

override fun jobExecutionVetoed(context: JobExecutionContext?) {
// do nothing
}

override fun jobWasExecuted(
context: JobExecutionContext?,
jobException: JobExecutionException?
) {
// do nothing
}
}

@Bean
open fun mySchedulerFactoryBeanCustomizer(): SchedulerFactoryBeanCustomizer {
return SchedulerFactoryBeanCustomizer { schedulerFactoryBean -> schedulerFactoryBean.setGlobalJobListeners(MyJobListener()) }
}
}

@Configuration(proxyBeanMethods = false)
open class CustomOptionsConfigurationConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.quartz.JobExecutionContext
import org.quartz.JobExecutionException
import org.quartz.JobListener
import org.quartz.Scheduler
import org.quartz.core.QuartzScheduler
import org.slf4j.MDC
import org.springframework.aop.support.NameMatchMethodPointcut
import org.springframework.boot.autoconfigure.AutoConfigurations
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
import org.springframework.boot.context.annotation.UserConfigurations
Expand Down Expand Up @@ -784,6 +789,61 @@ class SentryAutoConfigurationTest {
}
}

@Test
fun `Sentry quartz job listener is added`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.enable-automatic-checkins=true")
.withUserConfiguration(QuartzAutoConfiguration::class.java)
.run {
val jobListeners = it.getBean(Scheduler::class.java).listenerManager.jobListeners
assertThat(jobListeners).hasSize(1)
assertThat(jobListeners[0]).matches(
{ it.name == "sentry-job-listener" },
"is sentry job listener"
)
}
}

@Test
fun `user defined SchedulerFactoryBeanCustomizer overrides Sentry Customizer`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.enable-automatic-checkins=true")
.withUserConfiguration(QuartzAutoConfiguration::class.java, CustomSchedulerFactoryBeanCustomizerConfiguration::class.java)
.run {
val jobListeners = it.getBean(Scheduler::class.java).listenerManager.jobListeners
assertThat(jobListeners).hasSize(1)
assertThat(jobListeners[0]).matches(
{ it.name == "custom-job-listener" },
"is custom job listener"
)
}
}

@Configuration(proxyBeanMethods = false)
open class CustomSchedulerFactoryBeanCustomizerConfiguration {
class MyJobListener : JobListener {
override fun getName() = "custom-job-listener"

override fun jobToBeExecuted(context: JobExecutionContext?) {
// do nothing
}

override fun jobExecutionVetoed(context: JobExecutionContext?) {
// do nothing
}

override fun jobWasExecuted(
context: JobExecutionContext?,
jobException: JobExecutionException?
) {
// do nothing
}
}

@Bean
open fun mySchedulerFactoryBeanCustomizer(): SchedulerFactoryBeanCustomizer {
return SchedulerFactoryBeanCustomizer { schedulerFactoryBean -> schedulerFactoryBean.setGlobalJobListeners(MyJobListener()) }
}
}

@Configuration(proxyBeanMethods = false)
open class CustomOptionsConfigurationConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Configuration(proxyBeanMethods = false)
@Open
@ApiStatus.Experimental
public class SentryQuartzConfiguration {

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() {
return new SentrySchedulerFactoryBeanCustomizer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Configuration(proxyBeanMethods = false)
@Open
@ApiStatus.Experimental
public class SentryQuartzConfiguration {

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() {
return new SentrySchedulerFactoryBeanCustomizer();
}
Expand Down