-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ritesh] [BAH-3191] Fix issue occured during openmrs version upgrade (#…
…12) * [ Ritesh ] | BAH-3086 - Segregate bahmni patient create and update events and Adding meta info of publishing openmrs object in event header. * [ Ritesh ] | BAH-3086 - Changes as per review comments * [ Ritesh ] | BAH-3086 - Replaces Header keys with Enum * [ Ritesh ] | BAH-3086 - Minor fix * [ Ritesh ] | BAH-3086 - Added non-null check in JMSMessageCreatorTest * Added workflow dispatch for publishing omod * [ Ritesh ] | BAH-3191 - Fix issue of publishing event after openmrs base image change to 2.5.12. * [ Ritesh ] | BAH-3191 - PR Feedback incorporated. * [ Ritesh ] | BAH-3191 - PR Feedback incorporated for test --------- Co-authored-by: Kavitha S <[email protected]> Co-authored-by: kavitha-sundararajan <[email protected]>
- Loading branch information
1 parent
a67910b
commit 8b30205
Showing
16 changed files
with
457 additions
and
304 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...in/java/org/bahmni/module/events/api/configuration/BahmniEventPublisherConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.bahmni.module.events.api.configuration; | ||
|
||
import org.bahmni.module.events.api.publisher.BahmniEventPublisher; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class BahmniEventPublisherConfiguration { | ||
|
||
@Bean | ||
public BahmniEventPublisher bahmniEventPublisher() { | ||
return new BahmniEventPublisher(); | ||
} | ||
} |
114 changes: 0 additions & 114 deletions
114
api/src/main/java/org/bahmni/module/events/api/configuration/EventConfiguration.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
.../main/java/org/bahmni/module/events/api/configuration/JMSEventPublisherConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.bahmni.module.events.api.configuration; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.bahmni.module.events.api.publisher.JMSEventPublisher; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jms.core.JmsTemplate; | ||
import org.springframework.jms.support.destination.DynamicDestinationResolver; | ||
import org.springframework.jndi.JndiObjectFactoryBean; | ||
|
||
import javax.jms.ConnectionFactory; | ||
|
||
@Conditional(JMSEventPublishingToggleCondition.class) | ||
@Configuration | ||
public class JMSEventPublisherConfiguration { | ||
|
||
@Bean | ||
public JndiObjectFactoryBean eventJndiObjectFactoryBean() { | ||
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); | ||
|
||
String jndiJMSResourceName = "jmsConnectionFactory"; | ||
jndiObjectFactoryBean.setJndiName("java:comp/env/" + jndiJMSResourceName); | ||
jndiObjectFactoryBean.setProxyInterface(ConnectionFactory.class); | ||
jndiObjectFactoryBean.setLookupOnStartup(true); | ||
|
||
return jndiObjectFactoryBean; | ||
} | ||
|
||
@Bean | ||
public DynamicDestinationResolver eventDestinationResolver() { | ||
return new DynamicDestinationResolver(); | ||
} | ||
|
||
@Bean | ||
public JmsTemplate jmsTemplate(JndiObjectFactoryBean eventJndiObjectFactoryBean, DynamicDestinationResolver eventDestinationResolver) { | ||
JmsTemplate jmsTemplate = new JmsTemplate(); | ||
jmsTemplate.setConnectionFactory((ConnectionFactory) eventJndiObjectFactoryBean.getObject()); | ||
jmsTemplate.setDestinationResolver(eventDestinationResolver); | ||
jmsTemplate.setPubSubDomain(true); | ||
return jmsTemplate; | ||
} | ||
|
||
@Bean | ||
public JMSEventPublisher jmsEventPublisher(JmsTemplate jmsTemplate) { | ||
return new JMSEventPublisher(jmsTemplate, new ObjectMapper()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 35 additions & 31 deletions
66
api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,66 @@ | ||
package org.bahmni.module.events.api.listener; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.bahmni.module.events.api.model.BahmniEventType; | ||
import org.bahmni.module.events.api.model.Event; | ||
import org.bahmni.module.events.api.publisher.BahmniEventPublisher; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.appointments.model.Appointment; | ||
import org.openmrs.module.appointments.web.mapper.AppointmentMapper; | ||
import org.springframework.aop.AfterReturningAdvice; | ||
import org.springframework.aop.MethodBeforeAdvice; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
import static org.bahmni.module.events.api.model.BahmniEventType.*; | ||
import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_APPOINTMENT_CREATED; | ||
import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_APPOINTMENT_UPDATED; | ||
|
||
public class AppointmentAdvice implements AfterReturningAdvice, ApplicationEventPublisherAware, MethodBeforeAdvice { | ||
|
||
private ApplicationEventPublisher eventPublisher; | ||
public class AppointmentAdvice implements AfterReturningAdvice, MethodBeforeAdvice { | ||
|
||
private final BahmniEventPublisher eventPublisher; | ||
private final ThreadLocal<Map<String,Integer>> threadLocal = new ThreadLocal<>(); | ||
private final String APPOINTMENT_ID_KEY = "appointmentId"; | ||
private final AppointmentMapper appointmentMapper; | ||
private final Set<String> adviceMethodNames = Sets.newHashSet("validateAndSave"); | ||
|
||
private final ApplicationContext applicationContext; | ||
public AppointmentAdvice() { | ||
this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class); | ||
this.appointmentMapper = Context.getRegisteredComponent("appointmentMapper", AppointmentMapper.class); | ||
} | ||
|
||
public AppointmentAdvice(ApplicationContext applicationContext) { | ||
this.applicationContext = applicationContext; | ||
public AppointmentAdvice(BahmniEventPublisher bahmniEventPublisher, AppointmentMapper appointmentMapper) { | ||
this.eventPublisher = bahmniEventPublisher; | ||
this.appointmentMapper = appointmentMapper; | ||
} | ||
|
||
@Override | ||
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { | ||
Map<String, Integer> appointmentInfo = threadLocal.get(); | ||
// TODO: This is a workaround to avoid publishing duplicate events because currently the event is getting called twice. Need to find out the reason and resolve it. | ||
if (appointmentInfo != null) { | ||
BahmniEventType eventType = appointmentInfo.get(APPOINTMENT_ID_KEY) == null ? BAHMNI_APPOINTMENT_CREATED : BAHMNI_APPOINTMENT_UPDATED; | ||
threadLocal.remove(); | ||
Appointment appointment = (Appointment) returnValue; | ||
AppointmentMapper appointmentMapper = applicationContext.getBean(AppointmentMapper.class); | ||
Object representation = appointmentMapper.constructResponse(appointment); | ||
Event event = new Event(eventType, representation, appointment.getUuid()); | ||
eventPublisher.publishEvent(event); | ||
System.out.println("Successfully published event with uuid : " + appointment.getUuid()); | ||
if (adviceMethodNames.contains(method.getName())) { | ||
Map<String, Integer> appointmentInfo = threadLocal.get(); | ||
// TODO: This is a workaround to avoid publishing duplicate events because currently the event is getting called twice. Need to find out the reason and resolve it. | ||
if (appointmentInfo != null) { | ||
BahmniEventType eventType = appointmentInfo.get(APPOINTMENT_ID_KEY) == null ? BAHMNI_APPOINTMENT_CREATED : BAHMNI_APPOINTMENT_UPDATED; | ||
threadLocal.remove(); | ||
Appointment appointment = (Appointment) returnValue; | ||
Object representation = appointmentMapper.constructResponse(appointment); | ||
Event event = new Event(eventType, representation, appointment.getUuid()); | ||
eventPublisher.publishEvent(event); | ||
System.out.println("Successfully published event with uuid : " + appointment.getUuid()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { | ||
this.eventPublisher = applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
public void before(Method method, Object[] objects, Object o) { | ||
Appointment appointment = ((Supplier<Appointment>) objects[0]).get(); | ||
Map<String, Integer> appointmentInfo = new HashMap<>(1); | ||
appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId()); | ||
threadLocal.set(appointmentInfo); | ||
if (adviceMethodNames.contains(method.getName())) { | ||
Appointment appointment = ((Supplier<Appointment>) objects[0]).get(); | ||
Map<String, Integer> appointmentInfo = new HashMap<>(1); | ||
appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId()); | ||
threadLocal.set(appointmentInfo); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.