问题描述: 在使用ActiveMQ经典嵌入式代理时,当磁盘已满时,tempStorageUsage未报告,并且出现错误。
解决方法:
vm://localhost?broker.persistent=false
以下是一个示例代码片段,用于检查磁盘空间,并在磁盘空间不足时停止发送消息:
import org.apache.activemq.broker.region.Destination;
import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerPluginSupport;
public class DiskSpaceCheckerPlugin extends BrokerPluginSupport {
private long minimumFreeSpace;
public DiskSpaceCheckerPlugin(long minimumFreeSpace) {
this.minimumFreeSpace = minimumFreeSpace;
}
@Override
public Broker installPlugin(Broker broker) throws Exception {
PolicyMap policyMap = new PolicyMap();
PolicyEntry policyEntry = new PolicyEntry();
policyEntry.setProducerFlowControl(false);
policyEntry.setStoreUsageHighWaterMark(99);
policyEntry.setSendFailIfNoSpace(true);
policyEntry.setSendFailIfNoSpaceAfterTimeout(5000);
policyEntry.setAdvisoryWhenFull(true);
policyMap.setDefaultEntry(policyEntry);
broker.setDestinationPolicy(policyMap);
broker.setPlugins(new BrokerPlugin[]{
new BrokerPlugin() {
@Override
public Destination addDestination(ConnectionContext context, ActiveMQDestination destination, boolean createIfTemporary) throws Exception {
checkFreeSpace();
return super.addDestination(context, destination, createIfTemporary);
}
}});
return broker;
}
private void checkFreeSpace() {
File file = new File("path/to/your/directory");
long freeSpace = file.getUsableSpace();
if (freeSpace < minimumFreeSpace) {
throw new IllegalStateException("Disk space is running low.");
}
}
}
您可以根据自己的需求调整代码中的路径和最小可用磁盘空间值。通过将此插件安装到ActiveMQ中,它将在发送消息之前检查磁盘空间,并在磁盘空间不足时抛出异常。
下一篇:ActiveMQ经典消费问题