本文介绍了以编程方式读取队列的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 queue.xml中定义了一个队列
file
<队列条目>
<队列>
<名称>搜寻器< /名称>
< rate> 1 / s< / rate>
< retry-parameters>
...
< task-retry-limit> 5< / task-retry-limit>
< / retry-parameters>
< / queue>
< / queue-entries>
在Java中获取队列我使用简单的代码
队列队列= QueueFactory.getQueue(crawler);
现在我需要获取 task-retry-limit
给定队列的值。可能吗?我还没有找到任何方法在javadoc中这样做。
解决方案
基于@Tim的解决方案Castelijns回答以编程方式读取和解析文件。
所需的工作。当然你可以使用其他库来映射XML到POJO。
示例 queue.xml
< queue-entries>
<队列>
<名称>搜寻器< /名称>
< rate> 1 / s< / rate>
< retry-parameters>
< min-backoff-seconds> 10< / min-backoff-seconds>
< max-backoff-seconds> 300< / max-backoff-seconds>
<最大倍频> 2< /最大倍频>
<任务重试限制> 5< /任务重试限制>
< / retry-parameters>
< / queue>
< / queue-entries>
如何读取值
Serializer serializer = new Persister();
文件源=新文件(WEB-INF / queue.xml);
QueueEntries queueEntries = serializer.read(QueueEntries.class,source);
int retryLimit = queueEntries.getQueue()。getRetryParameters()。getTaskRetryLimit());
QueueEntries
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
public class QueueEntries {
@Element
private队列队列;
public Queue getQueue(){
return queue;
$ / code $ / pre
$ b $ p $ Que
import org.simpleframework.xml.Element;
公共类队列{
@元素
私有字符串速率;
@Element
私人字符串名称;
@Element(name =retry-parameters)
private RetryParameters retryParameters;
public String getRate(){
return rate;
}
public String getName(){
return name;
}
public RetryParameters getRetryParameters(){
return retryParameters;
重试参数
import org.simpleframework.xml.Element;
public class RetryParameters {
@Element(name =min-backoff-seconds)
private String minBackoffSeconds;
@Element(name =max-backoff-seconds)
private String maxBackoffSeconds;
@Element(name =max-doublings)
private String maxDoublings;
@Element(name =task-retry-limit)
private String taskRetryLimit;
public String getMinBackoffSeconds(){
return minBackoffSeconds;
}
public String getMaxDoublings(){
return maxDoublings;
}
public String getMaxBackoffSeconds(){
return maxBackoffSeconds;
}
public String getTaskRetryLimit(){
return taskRetryLimit;
}
}
I've got a queue defined in queue.xml
file
<queue-entries>
<queue>
<name>crawler</name>
<rate>1/s</rate>
<retry-parameters>
...
<task-retry-limit>5</task-retry-limit>
</retry-parameters>
</queue>
</queue-entries>
To obtain a queue in Java I'm using simple code
Queue queue = QueueFactory.getQueue("crawler");
Now I need to obtain the task-retry-limit
value for given queue. Is it possible? I haven't found any method to do so in javadoc.
解决方案 Solution based on @Tim Castelijns answer to read and parse the file programmatically.
SimpleXML in needed for this to work. Of course you can use other library to map XML to POJO.
Sample queue.xml
<queue-entries>
<queue>
<name>crawler</name>
<rate>1/s</rate>
<retry-parameters>
<min-backoff-seconds>10</min-backoff-seconds>
<max-backoff-seconds>300</max-backoff-seconds>
<max-doublings>2</max-doublings>
<task-retry-limit>5</task-retry-limit>
</retry-parameters>
</queue>
</queue-entries>
How to read value
Serializer serializer = new Persister();
File source = new File("WEB-INF/queue.xml");
QueueEntries queueEntries = serializer.read(QueueEntries.class, source);
int retryLimit = queueEntries.getQueue().getRetryParameters().getTaskRetryLimit());
QueueEntries
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
public class QueueEntries {
@Element
private Queue queue;
public Queue getQueue() {
return queue;
}
}
Queue
import org.simpleframework.xml.Element;
public class Queue {
@Element
private String rate;
@Element
private String name;
@Element(name = "retry-parameters")
private RetryParameters retryParameters;
public String getRate() {
return rate;
}
public String getName() {
return name;
}
public RetryParameters getRetryParameters() {
return retryParameters;
}
}
RetryParameters
import org.simpleframework.xml.Element;
public class RetryParameters {
@Element(name = "min-backoff-seconds")
private String minBackoffSeconds;
@Element(name = "max-backoff-seconds")
private String maxBackoffSeconds;
@Element(name = "max-doublings")
private String maxDoublings;
@Element(name = "task-retry-limit")
private String taskRetryLimit;
public String getMinBackoffSeconds() {
return minBackoffSeconds;
}
public String getMaxDoublings() {
return maxDoublings;
}
public String getMaxBackoffSeconds() {
return maxBackoffSeconds;
}
public String getTaskRetryLimit() {
return taskRetryLimit;
}
}
这篇关于以编程方式读取队列的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!