Skip to content

Commit

Permalink
Optimize the logic of DatabaseMessageSender. (#4809)
Browse files Browse the repository at this point in the history
* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* ref(apollo-biz): Optimize the logic of DatabaseMessageSender.

---------

Co-authored-by: Jason Song <[email protected]>
  • Loading branch information
klboke and nobodyiam authored Mar 21, 2023
1 parent 196c8f9 commit a441b6b
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.google.common.collect.Queues;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand All @@ -43,7 +44,7 @@
public class DatabaseMessageSender implements MessageSender {
private static final Logger logger = LoggerFactory.getLogger(DatabaseMessageSender.class);
private static final int CLEAN_QUEUE_MAX_SIZE = 100;
private BlockingQueue<Long> toClean = Queues.newLinkedBlockingQueue(CLEAN_QUEUE_MAX_SIZE);
private final BlockingQueue<Long> toClean = Queues.newLinkedBlockingQueue(CLEAN_QUEUE_MAX_SIZE);
private final ExecutorService cleanExecutorService;
private final AtomicBoolean cleanStopped;

Expand All @@ -68,7 +69,9 @@ public void sendMessage(String message, String channel) {
Transaction transaction = Tracer.newTransaction("Apollo.AdminService", "sendMessage");
try {
ReleaseMessage newMessage = releaseMessageRepository.save(new ReleaseMessage(message));
toClean.offer(newMessage.getId());
if(!toClean.offer(newMessage.getId())){
logger.warn("Queue is full, Failed to add message {} to clean queue", newMessage.getId());
}
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
logger.error("Sending message to database failed", ex);
Expand Down Expand Up @@ -116,6 +119,7 @@ private void cleanMessage(Long id) {
}
}

@PreDestroy
void stopClean() {
cleanStopped.set(true);
}
Expand Down

0 comments on commit a441b6b

Please sign in to comment.