所以我正在尝试设置一个嵌入式jetty 9服务器。但是,当我尝试:
mvn clean compile exec:java
执行暂停于:
INFO:oejs.Server:com.main.SimpleServer.main():jetty-9.0.0.RC2
过了一会儿,我得到了例外情况:
警告:oejuc.AbstractLifeCycle:com.main.SimpleServer.main():FAILED org.eclipse.jetty.io.SelectorManager $ ManagedSelector@45a0110e keys = -1 selected = -1:java.io.IOException:无法建立环回连接
java.io.IOException:无法建立环回连接 java.net.ConnectException:连接被拒绝:连接
我的代码如下所示:
public class HelloHandler extends AbstractHandler {
final String _greeting;
final String _body;
public HelloHandler() {
_greeting = "Hello World";
_body = null;
}
public HelloHandler(String greeting) {
_greeting = greeting;
_body = null;
}
public HelloHandler(String greeting, String body) {
_greeting = greeting;
_body = body;
}
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>" + _greeting + "</h1>");
if (_body != null) response.getWriter().println(_body);
}
}
和
public class SimpleServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
server.setHandler(new HelloHandler());
server.start();
server.join();
}
}
我的pom.xml看起来像:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestJetty</groupId>
<artifactId>TestJetty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<jettyVersion>9.0.0.RC2</jettyVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<version>7.4.4.v20110707</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<mainClass>com.main.SimpleServer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:1)
这意味着您的localhost环回要么配置错误(例如报告为非环回IP地址),要么被某些东西阻止使用(最常见的原因是微软Windows策略决策和防火墙规则)。
不常见的是没有IPv4可用的计算机(仅限IPv6)。
尝试编译并运行此...
package stackoverflow.jetty;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
public class AddrList
{
public static void main(String[] args)
{
try
{
InetAddress loopback = InetAddress.getLoopbackAddress();
dump("Loopback",loopback);
}
catch (Throwable t)
{
System.out.println("Unable to getLoopbackAddress()");
t.printStackTrace();
}
try
{
InetAddress localhost = InetAddress.getLocalHost();
dump("LocalHost",localhost);
}
catch (Throwable t)
{
System.out.println("Unable to getLocalHost()");
t.printStackTrace();
}
try
{
InetAddress alladdr = InetAddress.getByName("0.0.0.0");
dump("0.0.0.0",alladdr);
}
catch (Throwable t)
{
System.out.println("Unable to getLocalHost()");
t.printStackTrace();
}
try
{
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface iface : Collections.list(nets))
{
try
{
System.out.println("DisplayName = " + iface.getDisplayName());
System.out.println("Name = " + iface.getName());
List<InetAddress> addrs = Collections.list(iface.getInetAddresses());
int i = 0;
for (InetAddress addr : addrs)
{
dump(Integer.toString(i++),addr);
}
}
catch (Throwable t)
{
System.out.println("Unable to InetAddress for NetworkInterface: " + iface.getDisplayName());
t.printStackTrace();
}
}
}
catch (SocketException e)
{
System.out.print("Unable to get all network interfaces");
e.printStackTrace();
}
}
public static void dump(String type, InetAddress addr)
{
String header = String.format("[%s] InetAddress",type);
try
{
System.out.println(header + " = " + addr);
System.out.println(header + ".isAnyLocalAddress = " + addr.isAnyLocalAddress());
System.out.println(header + ".isLinkLocalAddress = " + addr.isLinkLocalAddress());
System.out.println(header + ".isLoopbackAddress = " + addr.isLoopbackAddress());
}
catch (Throwable t)
{
System.out.printf("[%s] Failed to list InetAddress details%n",type);
t.printStackTrace();
}
try
{
header = String.format("[%s] InetSocketAddress",type);
InetSocketAddress isockaddr = new InetSocketAddress(addr,8080);
System.out.println(header + " = " + isockaddr);
System.out.println(header + ".isUnresolved = " + isockaddr.isUnresolved());
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
这应该列出您计算机上所有网络接口的变体。 如果您有错误或异常,则需要首先解决,然后才能在该计算机上启动Jetty。
在普通机器上,您会看到以下输出:
[Loopback] InetAddress = localhost/127.0.0.1
[Loopback] InetAddress.isAnyLocalAddress = false
[Loopback] InetAddress.isLinkLocalAddress = false
[Loopback] InetAddress.isLoopbackAddress = true
[Loopback] InetSocketAddress = localhost/127.0.0.1:8080
[Loopback] InetSocketAddress.isUnresolved = false
[LocalHost] InetAddress = lapetus/127.0.1.1
[LocalHost] InetAddress.isAnyLocalAddress = false
[LocalHost] InetAddress.isLinkLocalAddress = false
[LocalHost] InetAddress.isLoopbackAddress = true
[LocalHost] InetSocketAddress = lapetus/127.0.1.1:8080
[LocalHost] InetSocketAddress.isUnresolved = false
[0.0.0.0] InetAddress = /0.0.0.0
[0.0.0.0] InetAddress.isAnyLocalAddress = true
[0.0.0.0] InetAddress.isLinkLocalAddress = false
[0.0.0.0] InetAddress.isLoopbackAddress = false
[0.0.0.0] InetSocketAddress = /0.0.0.0:8080
[0.0.0.0] InetSocketAddress.isUnresolved = false
(snip ... long list of other interfaces)
这向我展示了所有3种选择:loopback,localhost和0.0.0.0(任何接口) 作为Jetty的听众工作。
答案 1 :(得分:-1)
Jetty服务器已配置并为我工作。在一个晴朗的日子,它开始给予&#34;无法建立环回连接&#34;错误。我重新启动了我的机器,它又开始工作了。所以我认为这个错误与Windows操作系统完全相关。