Jetty 9.x的连接器中的连接对象到哪里去了? [英] Where did the Connection object in the Connectors in Jetty 9.x go?
问题描述
当我尝试从Jetty 8.1.12迁移到Jetty 9.4.x时,由于以下问题,我收到了错误消息.
我们使用connector.getConnection()方法.而且我在Jetty 9.4.x中没有找到任何替代品.
这是关于码头升级8到9
我们使用connector.getConnection()获取inet套接字地址.
private InetSocketAddress findFirstInetConnector(Server server) {
Connector[] connectors = server.getConnectors();
if (connectors != null) {
for (Connector connector : connectors) {
Object connection = connector.getConnection();
if (connection instanceof ServerSocketChannel) {
SocketAddress address = ((ServerSocketChannel) connector.getConnection()).socket().getLocalSocketAddress();
if (address instanceof InetSocketAddress) {
return (InetSocketAddress) address;
}
} else if (connection instanceof ServerSocket) {
SocketAddress address = ((ServerSocket) connector.getConnection()).getLocalSocketAddress();
if (address instanceof InetSocketAddress) {
return (InetSocketAddress) address;
}
}
}
}
return null;
}
任何人都可以帮助我找到相同的替代品.
连接器不再具有连接.
您不能再随意从中取出SocketAddress
或InetSocketAddress
.
为什么?
好吧,我们有逻辑连接器,物理连接器,非网络连接器,网络连接器,服务器连接器,本地连接器,基于管道的连接器,基于jni的连接器,基于unixsocket的连接器等...
您必须根据已启动和正在运行连接器(例如NetworkConnector localPort和主机)上的信息来创建所需的InetSocketAddress
.>
要遍历连接器信息,您可以...
for (Connector connector : server.getConnectors())
{
// What is the connector name (can be null or unset)
String connectorName = connector.getName();
// What is the declared protocol default for this connector?
String defaultProtocol = connector.getDefaultConnectionFactory().getProtocol();
// What other features does this connector handle?
for (ConnectionFactory connectionFactory : connector.getConnectionFactories())
{
// List of protocols handled by this specific connection factory for this specific connector
connectionFactory.getProtocols();
if (connectionFactory instanceof SslConnectionFactory)
{
// this can handle TLS/SSL based connections
}
if (connectionFactory instanceof HttpConnectionFactory)
{
// this can handle http protocols
// get the http specific configuration
HttpConfiguration httpConfig = ((HttpConnectionFactory) connectionFactory).getHttpConfiguration();
// what port is recognized as secure
httpConfig.getSecurePort();
// what scheme is recognized as secure
httpConfig.getSecureScheme();
}
if (connectionFactory instanceof HTTP2ServerConnectionFactory)
{
// can handle encrypted http/2 protocols (and alpn features)
}
if (connectionFactory instanceof HTTP2CServerConnectionFactory)
{
// this can handle http/2's special clear-text "h2c" protocol (no alpn features)
}
}
if (!connector.isRunning())
{
// no information below can be trusted unless the connector is started
continue;
}
if (connector instanceof NetworkConnector)
{
// we have a network capable connector
NetworkConnector networkConnector = (NetworkConnector) connector;
// What interface is it listening on?
String interfaceName = networkConnector.getHost();
// What local port is it bound to?
int localPort = networkConnector.getLocalPort();
}
}
When I am trying to migrate to Jetty 9.4.x from Jetty 8.1.12, I am getting errors because of following issue.
We use connector.getConnection() method. And I did not find any replacement in Jetty 9.4.x.
This is followup question on Jetty upgrade 8 to 9
We use connector.getConnection() to get inet socket address.
private InetSocketAddress findFirstInetConnector(Server server) {
Connector[] connectors = server.getConnectors();
if (connectors != null) {
for (Connector connector : connectors) {
Object connection = connector.getConnection();
if (connection instanceof ServerSocketChannel) {
SocketAddress address = ((ServerSocketChannel) connector.getConnection()).socket().getLocalSocketAddress();
if (address instanceof InetSocketAddress) {
return (InetSocketAddress) address;
}
} else if (connection instanceof ServerSocket) {
SocketAddress address = ((ServerSocket) connector.getConnection()).getLocalSocketAddress();
if (address instanceof InetSocketAddress) {
return (InetSocketAddress) address;
}
}
}
}
return null;
}
Can anyone help me find replacement for the same.
A Connector has no Connection anymore.
You cannot just arbitrarily get a SocketAddress
or InetSocketAddress
out of it anymore.
Why?
Well, we have logical connectors, physical connectors, non-network connectors, network connectors, server connectors, local connectors, pipe based connectors, jni based connectors, unixsocket based connectors, etc...
You'll have to create the InetSocketAddress
you want from the information on a started and running connector (such as the NetworkConnector localPort and host).
To walk the connector information you can ...
for (Connector connector : server.getConnectors())
{
// What is the connector name (can be null or unset)
String connectorName = connector.getName();
// What is the declared protocol default for this connector?
String defaultProtocol = connector.getDefaultConnectionFactory().getProtocol();
// What other features does this connector handle?
for (ConnectionFactory connectionFactory : connector.getConnectionFactories())
{
// List of protocols handled by this specific connection factory for this specific connector
connectionFactory.getProtocols();
if (connectionFactory instanceof SslConnectionFactory)
{
// this can handle TLS/SSL based connections
}
if (connectionFactory instanceof HttpConnectionFactory)
{
// this can handle http protocols
// get the http specific configuration
HttpConfiguration httpConfig = ((HttpConnectionFactory) connectionFactory).getHttpConfiguration();
// what port is recognized as secure
httpConfig.getSecurePort();
// what scheme is recognized as secure
httpConfig.getSecureScheme();
}
if (connectionFactory instanceof HTTP2ServerConnectionFactory)
{
// can handle encrypted http/2 protocols (and alpn features)
}
if (connectionFactory instanceof HTTP2CServerConnectionFactory)
{
// this can handle http/2's special clear-text "h2c" protocol (no alpn features)
}
}
if (!connector.isRunning())
{
// no information below can be trusted unless the connector is started
continue;
}
if (connector instanceof NetworkConnector)
{
// we have a network capable connector
NetworkConnector networkConnector = (NetworkConnector) connector;
// What interface is it listening on?
String interfaceName = networkConnector.getHost();
// What local port is it bound to?
int localPort = networkConnector.getLocalPort();
}
}
这篇关于Jetty 9.x的连接器中的连接对象到哪里去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!