Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(apollo-biz): Optimize the logic of DatabaseMessageSender.
Browse files Browse the repository at this point in the history
klboke committed Mar 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent aa95cdd commit 468c415
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;

@@ -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);
@@ -116,6 +119,7 @@ private void cleanMessage(Long id) {
}
}

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

0 comments on commit 468c415

Please sign in to comment.