yunfan24 commented on issue #3047:
URL: https://github.com/apache/hertzbeat/issues/3047#issuecomment-2634538424

   Since the warehouse module does not import the manager module dependency, it 
cannot directly use the MonitorServiceImpl methods to update the status, and 
globally disabling the JPA cache would have a significant impact.   I have used 
the following approach to resolve the issue.
   
   In DataStorageDispatch, I introduced EntityManager and TransactionTemplate, 
executing the calculateMonitorStatus method within a transactional context and 
refreshing the corresponding cache using EntityManager.
   
   Are there any issues with this approach, or is there a better solution?   If 
necessary, feel free to assign it to me.
   
   ```java
   protected void startPersistentDataStorage() {
       Runnable runnable = () -> {
           Thread.currentThread().setName("warehouse-persistent-data-storage");
           while (!Thread.currentThread().isInterrupted()) {
               try {
                   CollectRep.MetricsData metricsData = 
commonDataQueue.pollMetricsDataToStorage();
                   if (metricsData == null) {
                       continue;
                   }
   //                calculateMonitorStatus(metricsData);
                   // Use TransactionTemplate to execute calculateMonitorStatus 
method in a transaction context
                   transactionTemplate.execute(status -> {
                       calculateMonitorStatus(metricsData);
                       return null;
                   });
                   historyDataWriter.ifPresent(dataWriter -> 
dataWriter.saveData(metricsData));
                   ........other code........
               }
           }
       };
       workerPool.executeJob(runnable);
   }
   
   @Transactional
   protected void calculateMonitorStatus(CollectRep.MetricsData metricsData) {
       if (metricsData.getPriority() == 0) {
           long id = metricsData.getId();
           CollectRep.Code code = metricsData.getCode();
           // query current status
           String queryStatusSql = "SELECT status FROM hzb_monitor WHERE id = 
?";
           try {
               int currentStatus = jdbcTemplate.queryForObject(queryStatusSql, 
Integer.class, id);
               if (code == CollectRep.Code.SUCCESS && currentStatus == 
CommonConstants.MONITOR_DOWN_CODE) {
                   // if collect success and current status is DOWN, update to 
UP
                   String sql = "UPDATE hzb_monitor SET status = ? WHERE id = 
?";
                   jdbcTemplate.update(sql, CommonConstants.MONITOR_UP_CODE, 
id);
                   
                   refreshEntity(id);
               ........other code........
   }
   
   // Refresh the cache of the entity with the specified ID
   private void refreshEntity(Long id) {
       Monitor monitor = entityManager.find(Monitor.class, id);
       if (monitor != null) {
           entityManager.refresh(monitor);
           
entityManager.getEntityManagerFactory().getCache().evict(Monitor.class, id);
       }
   }
   ```


-- 
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: notifications-unsubscr...@hertzbeat.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@hertzbeat.apache.org
For additional commands, e-mail: notifications-h...@hertzbeat.apache.org

Reply via email to