|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.modulith.events.jobrunr; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.jobrunr.scheduling.JobScheduler; |
| 21 | +import org.springframework.context.ApplicationListener; |
| 22 | +import org.springframework.context.PayloadApplicationEvent; |
| 23 | +import org.springframework.modulith.events.EventExternalizationConfiguration; |
| 24 | +import org.springframework.modulith.events.core.ConditionalEventListener; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Oliver Drotbohm |
| 29 | + */ |
| 30 | +class JobRunrEventExternalizer |
| 31 | + implements ApplicationListener<PayloadApplicationEvent<?>>, ConditionalEventListener { |
| 32 | + |
| 33 | + private final JobScheduler scheduler; |
| 34 | + private final List<JobRunrExternalizationTransport> externalizers; |
| 35 | + private final EventExternalizationConfiguration configuration; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new {@link JobRunrEventExternalizer} for the given {@link JobScheduler}, |
| 39 | + * {@link JobRunrExternalizationTransport}s and {@link EventExternalizationConfiguration}. |
| 40 | + * |
| 41 | + * @param scheduler must not be {@literal null}. |
| 42 | + * @param externalizers must not be {@literal null}. |
| 43 | + * @param configuration must not be {@literal null}. |
| 44 | + */ |
| 45 | + JobRunrEventExternalizer(JobScheduler scheduler, List<JobRunrExternalizationTransport> externalizers, |
| 46 | + EventExternalizationConfiguration configuration) { |
| 47 | + |
| 48 | + Assert.notNull(scheduler, "JobScheduler must not be null!"); |
| 49 | + Assert.notNull(externalizers, "JobRunrExternalizationTransports must not be null!"); |
| 50 | + Assert.notNull(configuration, "EventExternalizationConfiguration must not be null!"); |
| 51 | + |
| 52 | + this.scheduler = scheduler; |
| 53 | + this.externalizers = externalizers; |
| 54 | + this.configuration = configuration; |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * (non-Javadoc) |
| 59 | + * @see org.springframework.modulith.events.core.ConditionalEventListener#supports(java.lang.Object) |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public boolean supports(Object event) { |
| 63 | + return configuration.supports(event); |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * (non-Javadoc) |
| 68 | + * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void onApplicationEvent(PayloadApplicationEvent<?> event) { |
| 72 | + |
| 73 | + var payload = event.getPayload(); |
| 74 | + |
| 75 | + scheduler.enqueue(externalizers.stream(), it -> it.externalize(payload)); |
| 76 | + } |
| 77 | +} |
0 commit comments