Aman-Mittal commented on code in PR #6025: URL: https://github.com/apache/fineract/pull/6025#discussion_r3473068287
########## fineract-core/src/main/java/org/apache/fineract/infrastructure/core/service/TransactionBoundApplicationEventPublisher.java: ########## @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.core.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +@Component +@RequiredArgsConstructor +@Slf4j +public class TransactionBoundApplicationEventPublisher { + + private final ApplicationEventPublisher applicationEventPublisher; + + public void publishEvent(Object event) { + if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { + + @Override + public void afterCommit() { + try { + doPublish(event); + } catch (Exception e) { + log.error("Error while publishing application event after transaction commit: {}", event, e); + } + } + }); + return; + } + doPublish(event); + } + + private void doPublish(Object event) { + applicationEventPublisher.publishEvent(event); Review Comment: Can this event be null? ########## fineract-core/src/main/java/org/apache/fineract/infrastructure/core/service/TransactionBoundApplicationEventPublisher.java: ########## @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.core.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +@Component +@RequiredArgsConstructor +@Slf4j +public class TransactionBoundApplicationEventPublisher { + + private final ApplicationEventPublisher applicationEventPublisher; + + public void publishEvent(Object event) { + if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { + + @Override + public void afterCommit() { + try { + doPublish(event); + } catch (Exception e) { Review Comment: since TransactionBoundApplicationEventPublisher already handles the "publish after commit or immediately if no transaction exists" logic, could ActiveMQNotificationEventPublisher reuse that instead of implementing similar TransactionSynchronizationManager logic again? It seems like there might be some duplication here. ``` // 1. In TransactionBoundApplicationEventPublisher.java if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { doPublish(event); } catch (Exception e) { log.error("Error while publishing application event after transaction commit: {}", event, e); } } }); return; } doPublish(event); // 2. In ActiveMQNotificationEventPublisher.java if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { send(notificationData); } catch (Exception e) { log.error("Error while sending ActiveMQ notification event after transaction commit", e); } } }); return; } send(notificationData); ``` ########## fineract-core/src/main/java/org/apache/fineract/infrastructure/core/service/TransactionBoundApplicationEventPublisher.java: ########## @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.core.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +@Component +@RequiredArgsConstructor +@Slf4j +public class TransactionBoundApplicationEventPublisher { + + private final ApplicationEventPublisher applicationEventPublisher; + + public void publishEvent(Object event) { + if (TransactionSynchronizationManager.isActualTransactionActive() && TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { Review Comment: Quick question: since each registered synchronization holds a reference to the event until transaction completion, have we considered what happens in large transactions that publish a lot of events? Not a leak, but it could cause memory growth until commit. Do we have any high-volume use cases here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
