Skip to content

Commit

Permalink
Merge pull request #15 from Bahmni/bug/no-proxy
Browse files Browse the repository at this point in the history
Fix & Monitoring add for intermittent unable to initialize proxy - no session error
  • Loading branch information
kalai-tw authored Apr 29, 2024
2 parents 1bd9b03 + ed4e469 commit 46ca7b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
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.hibernate.SessionFactory;
import org.openmrs.Encounter;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.web.mapper.AppointmentMapper;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;
import java.util.HashMap;
Expand All @@ -29,35 +26,48 @@ public class EncounterAdvice implements AfterReturningAdvice, MethodBeforeAdvice
private final Logger log = LogManager.getLogger(EncounterAdvice.class);

private final BahmniEventPublisher eventPublisher;
private final SessionFactory sessionFactory;
private final ThreadLocal<Map<String,Integer>> threadLocal = new ThreadLocal<>();
private final String ENCOUNTER_ID_KEY = "encounterId";
private final Set<String> adviceMethodNames = Sets.newHashSet("saveEncounter");

public EncounterAdvice() {
this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class);
this.sessionFactory = Context.getRegisteredComponent("sessionFactory", SessionFactory.class);
}

public EncounterAdvice(BahmniEventPublisher bahmniEventPublisher) {
public EncounterAdvice(BahmniEventPublisher bahmniEventPublisher,SessionFactory sessionFactory) {
this.eventPublisher = bahmniEventPublisher;
this.sessionFactory = sessionFactory;
}

@Override
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) {
if (adviceMethodNames.contains(method.getName())) {
Map<String, Integer> encounterInfo = 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 (encounterInfo != null) {
BahmniEventType eventType = encounterInfo.get(ENCOUNTER_ID_KEY) == null ? BAHMNI_ENCOUNTER_CREATED : BAHMNI_ENCOUNTER_UPDATED;
threadLocal.remove();
Encounter encounter = (Encounter) returnValue;
try {
if (adviceMethodNames.contains(method.getName())) {
Map<String, Integer> encounterInfo = 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 (encounterInfo != null) {
BahmniEventType eventType = encounterInfo.get(ENCOUNTER_ID_KEY) == null ? BAHMNI_ENCOUNTER_CREATED : BAHMNI_ENCOUNTER_UPDATED;
threadLocal.remove();
Encounter encounter = (Encounter) returnValue;

Object representation = ConversionUtil.convertToRepresentation(encounter, Representation.FULL);
Event event = new Event(eventType, representation, encounter.getUuid());
eventPublisher.publishEvent(event);
Object representation = ConversionUtil.convertToRepresentation(encounter, Representation.FULL);
Event event = new Event(eventType, representation, encounter.getUuid());
eventPublisher.publishEvent(event);

System.out.println("Successfully published event with uuid : " + encounter.getUuid());
System.out.println("Successfully published event with uuid : " + encounter.getUuid());
}
}
}
catch(Exception exception){
log.error("Error in Bahmni events EncounterAdvice while sending Encounter event : ", exception);
// ToDo : Remove below info logs once no session issue is completely resolved.
Encounter encounter = (Encounter)returnValue;
log.info("DEBUG : Encounter with no session issue " +encounter.toString());
log.info("DEBUG : Is Encounter Entity available in session ???? " +this.sessionFactory.getCurrentSession().contains(encounter));
log.info("DEBUG : Statics hibernate ***** "+ this.sessionFactory.getStatistics());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.hibernate.SessionFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -30,13 +31,15 @@ public class EncounterAdviceTest {

private EncounterAdvice encounterAdvice;
private BahmniEventPublisher bahmniEventPublisher;
private SessionFactory sessionFactory;

private final String ENCOUNTER_SAVE_METHOD_NAME = "saveEncounter";

@Before
public void setUp() {
bahmniEventPublisher = mock(BahmniEventPublisher.class);
encounterAdvice = new EncounterAdvice(bahmniEventPublisher);
sessionFactory = mock(SessionFactory.class);
encounterAdvice = new EncounterAdvice(bahmniEventPublisher,sessionFactory);
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

<require_modules>
<require_module>org.openmrs.module.webservices.rest</require_module>
<require_module>org.openmrs.module.emrapi</require_module>
<require_module>org.bahmni.module.bahmnicore</require_module>
<require_module>org.bahmni.module.appointments</require_module>
<require_module>org.openmrs.module.bacteriology</require_module>
</require_modules>

<advice>
Expand Down

0 comments on commit 46ca7b2

Please sign in to comment.