以编程方式读取队列的参数

时间:2015-04-21 14:50:23

标签: java google-app-engine

我在queue.xml文件中定义了一个队列

<queue-entries>
    <queue>
        <name>crawler</name>
        <rate>1/s</rate>
        <retry-parameters>
            ...
            <task-retry-limit>5</task-retry-limit>
        </retry-parameters>
    </queue>
</queue-entries>

使用简单代码

在Java中获取队列
Queue queue = QueueFactory.getQueue("crawler");

现在我需要获取给定队列的task-retry-limit值。可能吗?我还没有找到任何方法在javadoc中这样做。

2 个答案:

答案 0 :(得分:3)

看来这是不可能的。

如果查看the source code,您会发现没有定义此类功能。

您可以检索的唯一属性是名称,Queue.getName();


我认为无法使用当前的appengine api检索它。

您调用它时的一种hacky方式可能是使用xml库解析queue.xml文件,并手动提取值。

答案 1 :(得分:0)

基于@Tim Castelijns的解决方案回答以编程方式读取和解析文件。

SimpleXML需要这项工作。当然,您可以使用其他库将XML映射到POJO。

示例/* Special class on .container surrounding .navbar, used for positioning it into place. */ .navbar-wrapper { position: absolute; top: 0; right: 0; width: 100% !important; left: 0; z-index: 20; } /* Flip around the padding for proper display in narrow viewports */ .navbar-wrapper > .container { padding-right: 0; padding-left: 0; } .navbar-wrapper .navbar { padding-right: 15px; padding-left: 15px; width: 100% !important; } .navbar-inverse { width: 100% !important; background-color: rgba(0, 0, 0, 0.5) !important; } .navbar-inverse .nav > li > a { color: #FFFFFF; } .navbar-inverse .nav > li > a:hover { background: none; } .navbar-brand a { color: #FFFFFF; } .main-header-background { background-size: 100% 100%; background-repeat: no-repeat; background-position: center; background-image: url('./images/stars/img-stars-2.jpg'); height: 620px; width: 100%; margin-bottom: 60px; }

<div class="navbar-wrapper">
<div class="container">

<nav class="navbar navbar-inverse">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-brand">
<a href="#">Brand</a>
</div>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
<li class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>

</div><!-- /.navbar-collapse -->
</nav>
</div>
</div>

<div class="main-header-background">
</div>  

如何阅读价值

queue.xml

QueueEntries

<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>

队列

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());

RetryParameters

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root
public class QueueEntries {

    @Element
    private Queue queue;

    public Queue getQueue() {
        return queue;
    }
}