两次查询后无法发布到网站

时间:2016-08-17 20:38:22

标签: java post httpclient bukkit

目前我有一个插件每隔十秒就会将数据发布到一个URL,但在第一次(有时是第二次)之后,它会使用以下信息崩溃服务器:

[11:26:25 ERROR]: The server has stopped responding!
[11:26:25 ERROR]: Please report this to http://www.spigotmc.org/
[11:26:25 ERROR]: Be sure to include ALL relevant console errors and Minecraft crash reports
[11:26:25 ERROR]: Spigot version: git-Spigot-e50d38c-02f4218 (MC: 1.10)
[11:26:25 ERROR]: ------------------------------
[11:26:25 ERROR]: Server thread dump (Look for plugins here before reporting to Spigot!):
[11:26:25 ERROR]: ------------------------------
[11:26:25 ERROR]: Current Thread: Server thread
[11:26:25 ERROR]:       PID: 17 | Suspended: false | Native: false | State: WAITING
[11:26:25 ERROR]:       Stack:
[11:26:25 ERROR]:               sun.misc.Unsafe.park(Native Method)
[11:26:25 ERROR]:               java.util.concurrent.locks.LockSupport.park(Unknown Source)
[11:26:25 ERROR]:               java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
[11:26:25 ERROR]:               org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:138)
[11:26:25 ERROR]:               org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306)
[11:26:25 ERROR]:               org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
[11:26:25 ERROR]:               org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192)
[11:26:25 ERROR]:               org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185)
[11:26:25 ERROR]:               org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107)
[11:26:25 ERROR]:               org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276)
[11:26:25 ERROR]:               org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263)
[11:26:25 ERROR]:               org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)
[11:26:25 ERROR]:               org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
[11:26:25 ERROR]:               org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
[11:26:25 ERROR]:               org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
[11:26:25 ERROR]:               org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
[11:26:25 ERROR]:               org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
[11:26:25 ERROR]:               org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
[11:26:25 ERROR]:               org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
[11:26:25 ERROR]:               me.timlampen.serverstats.ServerStats.request(ServerStats.java:152)
[11:26:25 ERROR]:               me.timlampen.serverstats.ServerStats.postToURL(ServerStats.java:142)
[11:26:25 ERROR]:               me.timlampen.serverstats.ServerStats$1.run(ServerStats.java:86)
[11:26:25 ERROR]:               org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71)
[11:26:25 ERROR]:               org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
[11:26:25 ERROR]:               net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:741)
[11:26:25 ERROR]:               net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399)
[11:26:25 ERROR]:               net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672)
[11:26:25 ERROR]:               net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571)
[11:26:25 ERROR]:               java.lang.Thread.run(Unknown Source)
[11:26:25 ERROR]: ------------------------------
[11:26:25 ERROR]: Entire Thread Dump:
[11:26:25 ERROR]: ------------------------------
[11:26:25 ERROR]: Current Thread: pool-6-thread-1
[11:26:25 ERROR]:       PID: 52 | Suspended: false | Native: false | State: WAITING
[11:26:25 ERROR]:       Stack:
[11:26:25 ERROR]:               sun.misc.Unsafe.park(Native Method)
[11:26:25 ERROR]:               java.util.concurrent.locks.LockSupport.park(Unknown Source)
[11:26:25 ERROR]:               java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
[11:26:25 ERROR]:               java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
[11:26:25 ERROR]:               java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
[11:26:25 ERROR]:               java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
[11:26:25 ERROR]: ------------------------------
[11:26:25 ERROR]: Current Thread: Reference Handler
[11:26:25 ERROR]:       PID: 2 | Suspended: false | Native: false | State: WAITING
[11:26:25 ERROR]:       Stack:
[11:26:25 ERROR]:               java.lang.Object.wait(Native Method)
[11:26:25 ERROR]:               java.lang.Object.wait(Unknown Source)
[11:26:25 ERROR]:               java.lang.ref.Reference.tryHandlePending(Unknown Source)
[11:26:25 ERROR]:               java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
[11:26:25 ERROR]: ------------------------------
[11:26:25 INFO]: Startup script './start.sh' does not exist! Stopping server.
[11:26:25 INFO]: Stopping server

这是我正在使用的方法:

public String postToURL(String url, List<NameValuePair> data) throws Exception {
    HttpPost request = new HttpPost(url);
    request.setHeader("key", key);
    request.setEntity(new UrlEncodedFormEntity(data, Consts.UTF_8));
    HttpResponse response = httpClient.execute(request);
    java.util.Scanner s = new java.util.Scanner(response.getEntity().getContent(), "UTF-8").useDelimiter("\\A");
    Bukkit.broadcastMessage(s.hasNext() + " - asfd");
    return s.hasNext() ? s.next() : "";
}

触发该代码的代码(如果需要):

new BukkitRunnable(){
                    @Override
                    public void run() {
                        PostResult result = null;
                        try {
                            result = gson.fromJson(postToURL("xxx", longRefresh), PostResult.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                            longRefresh.clear();
                        }
                        if(result==null){
                            Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Unable to recieve information from the webserver (1)");
                        }
                        else if(result.success){
                            for(String cmd : result.commands){
                                cmd = cmd.contains("/") ? cmd.replace("/", "") : cmd;
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
                                Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Recieved reply from web: " + cmd);
                            }
                        }
                        else{
                            Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Unable to recieve information from the webserver (2)");
                        }
                    }
                }.runTaskLaterAsynchronously(p, 1);

截取网址。

POST可以通过前两次,但在此之后它才会重新加载服务器。

任何帮助都会很棒, 添

1 个答案:

答案 0 :(得分:0)

F我不太了解http,但我知道你正在学习一些沮丧的做法。

将bukkit调度程序与Runnables一起使用而不是BukkitRunnables

Bukkit.getSchedular.scheduleSyncRepeatingTask(plugin, new Runnable(){}, 0L, 200L);

这段代码将帮助您安排在0秒后开始并每十秒重复一次的任务。您也可以通过一个小的更改让它以异步方式运行

Bukkit.getSchedular.scheduleAsyncRepeatingTask(plugin, new Runnable(){}, 0L, 200L);

运行它ASYNCHRONOUSLY将防止错误导致服务器崩溃,因为服务器和您的任务将在不同的线程上运行。

我还注意到你添加了一个

throws Exception;

确保您捕获所有这些异常并且不允许它们逃离服务器,因为它们也可能是原因。