- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 rest api,它从其他 api 收集数据,用它做一些逻辑并发送回客户端:
我的主课:
public class Main {
public static void main(String[] args) {
Vertx.vertx().deployVerticle(RestVerticle.class.getName());
}
这是我的 RestVerticle:
public class RestVerticle extends AbstractVerticle {
public static final String API_V1 = "/api/v1";
private Map<String, JsonObject> products = new HashMap<>();
@Override
public void start() {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.get(API_V1 + "/business/all").handler(this::getAllBusinesses);
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
private void getAllBusinesses(RoutingContext routingContext) {
vertx.deployVerticle(YelpClientVerticle.class.getName());
MessageConsumer<String> consumer = vertx.eventBus().consumer("api");
consumer.handler(message -> {
JsonObject m = new JsonObject((String) message.body());
System.out.println("Received message: " + message.body());
routingContext.response().putHeader("content-type", "application/json").end(m.encodePrettily());
});
}
这是我的 httpclient ,它调用 Yelp API:
public class YelpClientVerticle extends AbstractVerticle {
private static final String API_HOST = "api.yelp.com";
private static final String DEFAULT_TERM = "dinner";
private static final String DEFAULT_LOCATION = "San Francisco, CA";
private static final int SEARCH_LIMIT = 20;
private static final String SEARCH_PATH = "/v2/search";
private static final String BUSINESS_PATH = "/v2/business";
/*
* Update OAuth credentials below from the Yelp Developers API site:
* http://www.yelp.com/developers/getting_started/api_access
*/
private static final String CONSUMER_KEY = "XXXXX";
private static final String CONSUMER_SECRET = "XXXXX";
private static final String TOKEN = "XXXXX";
private static final String TOKEN_SECRET = "XXXXX";
OAuthService service;
Token accessToken;
/**
* Setup the Yelp API OAuth credentials.
*
* @param consumerKey Consumer key
* @param consumerSecret Consumer secret
* @param token Token
* @param tokenSecret Token secret
*/
/**
* Creates and sends a request to the Search API by term and location.
* <p>
* See <a href="http://www.yelp.com/developers/documentation/v2/search_api">Yelp Search API V2</a>
* for more info.
*
* @param term <tt>String</tt> of the search term to be queried
* @param location <tt>String</tt> of the location
* @return <tt>String</tt> JSON Response
*/
public String searchForBusinessesByLocation(String term, String location) {
OAuthRequest request = createOAuthRequest(SEARCH_PATH);
request.addQuerystringParameter("term", term);
request.addQuerystringParameter("location", location);
request.addQuerystringParameter("limit", String.valueOf(SEARCH_LIMIT));
return sendRequestAndGetResponse(request);
}
/**
* Creates and sends a request to the Business API by business ID.
* <p>
* See <a href="http://www.yelp.com/developers/documentation/v2/business">Yelp Business API V2</a>
* for more info.
*
* @param businessID <tt>String</tt> business ID of the requested business
* @return <tt>String</tt> JSON Response
*/
public String searchByBusinessId(String businessID) {
OAuthRequest request = createOAuthRequest(BUSINESS_PATH + "/" + businessID);
return sendRequestAndGetResponse(request);
}
/**
* Creates and returns an {@link OAuthRequest} based on the API endpoint specified.
*
* @param path API endpoint to be queried
* @return <tt>OAuthRequest</tt>
*/
private OAuthRequest createOAuthRequest(String path) {
OAuthRequest request = new OAuthRequest(Verb.GET, "https://" + API_HOST + path);
return request;
}
/**
* Sends an {@link OAuthRequest} and returns the {@link Response} body.
*
* @param request {@link OAuthRequest} corresponding to the API request
* @return <tt>String</tt> body of API response
*/
private String sendRequestAndGetResponse(OAuthRequest request) {
System.out.println("Querying " + request.getCompleteUrl() + " ...");
this.service.signRequest(this.accessToken, request);
Response response = request.send();
return response.getBody();
}
/**
* Queries the Search API based on the command line arguments and takes the first result to query
* the Business API.
*
* @param yelpApiCli <tt>YelpAPICLI</tt> command line arguments
*/
private String queryAPI(YelpAPICLI yelpApiCli) {
String searchResponseJSON =
searchForBusinessesByLocation(yelpApiCli.term, yelpApiCli.location);
JSONParser parser = new JSONParser();
JSONObject response = null;
try {
response = (JSONObject) parser.parse(searchResponseJSON);
} catch (ParseException pe) {
System.out.println("Error: could not parse JSON response:");
System.out.println(searchResponseJSON);
System.exit(1);
}
JSONArray businesses = (JSONArray) response.get("businesses");
JSONObject firstBusiness = (JSONObject) businesses.get(0);
String firstBusinessID = firstBusiness.get("id").toString();
System.out.println(String.format(
"%s businesses found, querying business info for the top result \"%s\" ...",
businesses.size(), firstBusinessID));
// Select the first business and display business details
String businessResponseJSON = searchByBusinessId(firstBusinessID.toString());
System.out.println(String.format("Result for business \"%s\" found:", firstBusinessID));
System.out.println(businessResponseJSON);
return businessResponseJSON;
}
/**
* Command-line interface for the sample Yelp API runner.
*/
private static class YelpAPICLI {
@Parameter(names = {"-q", "--term"}, description = "Search Query Term")
public String term = DEFAULT_TERM;
@Parameter(names = {"-l", "--location"}, description = "Location to be Queried")
public String location = DEFAULT_LOCATION;
}
@Override
public void start() throws Exception {
// Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks.
this.service =
new ServiceBuilder().provider(TwoStepOAuth.class).apiKey(CONSUMER_KEY)
.apiSecret(CONSUMER_SECRET).build();
this.accessToken = new Token(TOKEN, TOKEN_SECRET);
YelpAPICLI yelpApiCli = new YelpAPICLI();
new JCommander(yelpApiCli);
String response = queryAPI(yelpApiCli);
vertx.eventBus().send("api", response);
}
我遇到了 2 个问题。
第一个问题是 Yelp 客户端处理请求的时间太长,它阻塞了主线程并发出警告:
Jan 04, 2016 1:34:30 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-4,5,main] has been blocked for 3151 ms, time limit is 2000
第二个问题是,在处理完第一个请求后,例如,我第一次访问我的 localhost:8080/api/v1/business/all ,请求成功返回,但下一次再次点击 URL时间,它抛出这样的异常:
Jan 04, 2016 1:34:30 AM io.vertx.core.eventbus.impl.HandlerRegistration
SEVERE: Failed to handleMessage
java.lang.IllegalStateException: Response has already been written
我该如何解决这两个问题?
最佳答案
问题是您在每个 请求时都在做所有工作——启动 Yelp Verticle,并在事件总线上注册消费者。这不可能是你想要的。
所以,我认为正在发生的事情:
getAllBusinesses()
方法被执行。YelpClientVerticle
。api
端点上注册,它将写入响应。 YelpClientVerticle
做了很多阻塞工作 - 这就是您收到 BlockedThreadChecker
警告的原因。 最后,Yelp 请求返回并通过事件总线发送一条消息,然后该消息被写入响应。
您提出另一个请求
转到 2
您的问题是,对于每个请求,您都会启动另一个 YelpClientVerticle
,并注册另一个处理程序来监听相同 EventBus
端点地址。
让多个处理程序监听同一个 EventBus 地址
是完全可以接受的。发生这种情况时,Vert.x
会以 RoundRobin
方式选择其中一个处理程序。
我猜测在第二个请求 中,Vertx
正在选择第一个处理程序,并尝试写入您已经写入的第一个请求的响应。因此错误。
我会尝试将 YelpClientVerticle
的部署移动到 RestVerticle
的启动中 - 那么您将只有一个实例。
您可能想要切换发送者/消费者,因此您发送
一条消息到 YelpClientVerticle
,然后它会回复。
您可能还想阅读有关 Running Blocking Code 的文档,因为您的 Yelp 客户端看起来正在阻塞。
希望对你有帮助
关于java - 顶点抛出 IllegalStateException : Response has already been written,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34580170/
我正在使用 SAF(存储访问框架)将文件写入 SD 卡。在 Marshmallow 上,文件的写入和更新实际上有很大的延迟(大约 10 秒)。 当我使用例如: android.support.v4.p
我不明白的意思 The original Java AWT was implemented by having widgets written in Java which delegated to p
我的 ASP.NET Core 应用程序使用“开箱即用”的外部登录身份验证。我想要实现的 - 在 facebook 挑战中,我想包装重定向 url 并将其作为 json 返回以在 jquery 前端使
我正在尝试安装 MySql Workbench 并下载 mysql-installer-web-community-5.7.13.0.msi。当我运行此文件时出现以下错误: "The instruct
考虑以下代码: #include int main() { int i = 0; printf("hello%n%d\n", &i, i); } 为什么打印hello0而不是hello5?
我是一名 Java/C++ 程序员,Ruby 是我的第一门脚本语言。有时我发现我在某些领域没有尽可能高效地使用它,例如: 目标:仅解析文件中的某些行。我要使用的模式是有一条非常大的线,尺寸大于 15,
尝试执行命令“vagrant up”时收到以下错误: --------------------------- VBoxHeadless.exe - Application Error --------
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我正在创建一个程序,需要从仍在写入的文件中读取。 主要问题是:如果使用在单独线程上运行的 InputStream 和 OutputStream 类执行读取和写入,有什么问题和问题为了防止数据损坏,我需
问题 我有一个 HTML5/JavaScript 应用程序。我想将它提交给苹果商店。 有没有办法将它嵌入到 Safari/Webkit 中,并让 apple 接受它作为应用程序? 上下文 这个项目最初
我正在文件 btree.h 中编写一个 btree 实现类“btree”,并在文件 btree_iterator.h 中使用外部迭代器类“btree_iterator”在 btree.tem 中实现,
我正在使用 Python 3.4 将 unicode 字符串写入文件。文件写入后,打开一看,完全是另外一组字符。 代码:- # -*- coding: utf-8 -*- with open('tes
我们编写的应用程序在 XP 中运行良好,但在迁移到 Vista 和 Windows 7 时遇到了严重的问题,这可能是由于用户数据的写入位置。 用例是这样的:个人用户需要登录机器并使用它来获取数据。主管
假设我有一个名为“REVIEWS”的表 此表包含客户为各种产品撰写的评论。 我希望能够对每个客户撰写的所有评论进行“计数”,所以我写: SELECT count(*) AS counter FROM
我正在使用一个应用程序创建一个 csv 文件,然后我想导出并在手机上阅读。但是,我将其保存到的位置不可见,并且难以传输。 有没有办法将其保存到更容易访问的位置,例如电话上的/documents ? (
我写了一个小工具,用于将给定目录下的所有目录和文件名打印到文件中。程序编译正常,但运行程序后,文件未写入。这对我来说看起来很奇怪。程序代码如下所示。 在代码的第49行,当我仅使用file作为方法的参数
所以我在 c 中有以下枚举方法: enum enum_type GetInfo (int socket, unsigned char *data) { } 在 api 中我可以找到这个: 接收到的数据
我不明白下面的 C 转换函数是如何工作的(以及为什么要这样写);我相当确定原作者知道他在做什么: typedef union TValue { uint64_t u64; double n;
这个问题已经有答案了: Node.js - Find home directory in platform agnostic way (6 个回答) 已关闭 3 年前。 我正在使用fs文件系统 JS
我正在构建一个 rest api,它从其他 api 收集数据,用它做一些逻辑并发送回客户端: 我的主课: public class Main { public static void main(Str
我是一名优秀的程序员,十分优秀!