JBoss AS 7 上的 HttpTunnelingServlet

Posted

技术标签:

【中文标题】JBoss AS 7 上的 HttpTunnelingServlet【英文标题】:HttpTunnelingServlet on JBoss AS 7 【发布时间】:2012-04-16 21:44:44 【问题描述】:

尝试在当前 JBoss 版本(AS 7.1.1.)上运行 JMS Servlet Transport Example,我在 Channel.connect() 上收到“java.net.ConnectException:连接被拒绝”。 如果在 HttpTunnelingServlet 中我将 LocalAddress 替换为 InetSocketAddress,则连接有效,但我想知道这是否是正确的方法。 此外,我读过org.jboss.netty.channel.socket.http,但不知道如何在 JBoss AS 7 上运行它。 任何建议,如何在 JBoss AS 7 上运行 HttpTunnelingServlet 将不胜感激。

非常感谢!


消息客户端:

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;


public class ServletTransportExample 
    public static void main(final String[] args) throws Exception 
        Connection connection = null;
        InitialContext initialContext = null;
        try 

            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    org.jboss.naming.remote.client.InitialContextFactory.class
                            .getName());
            env.put(Context.PROVIDER_URL, "remote://localhost:4447");
            env.put(Context.SECURITY_PRINCIPAL, "admin");
            env.put(Context.SECURITY_CREDENTIALS, "test");

            // Step 1. Create an initial context to perform the JNDI lookup.
            initialContext = new InitialContext(env);

            // Step 2. Perfom a lookup on the queue
            Queue queue = (Queue) initialContext.lookup("jms/queue/testQueue");

            // Step 3. Perform a lookup on the Connection Factory
            ConnectionFactory cf = (ConnectionFactory) initialContext
                    .lookup("jms/ServletConnectionFactory");

            // Step 4.Create a JMS Connection
            connection = cf.createConnection("admin", "test");

            System.out.println("connection created: " + connection);

            // Step 5. Create a JMS Session
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            // Step 6. Create a JMS Message Producer
            MessageProducer producer = session.createProducer(queue);

            // Step 7. Create a Text Message
            TextMessage message = session
                    .createTextMessage("This is a text message");

            System.out.println("Sent message: " + message.getText());

            // Step 8. Send the Message
            producer.send(message);

            // Step 9. Create a JMS Message Consumer
            MessageConsumer messageConsumer = session.createConsumer(queue);

            // Step 10. Start the Connection
            connection.start();

            // Step 11. Receive the message
            TextMessage messageReceived = (TextMessage) messageConsumer
                    .receive(5000);

            System.out.println("Received message: " + messageReceived.getText());

         catch (Exception e) 
            e.printStackTrace();
         finally 
            // Step 12. Be sure to close our JMS resources!
            if (initialContext != null) 
                initialContext.close();
            
            if (connection != null) 
                connection.close();
            
        
    


隧道战争的 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>HornetQServlet</servlet-name>
    <servlet-class>org.jboss.netty.channel.socket.http.HttpTunnelingServlet</servlet-class>
    <init-param>
      <javaee:param-name>endpoint</javaee:param-name>
      <javaee:param-value>local:org.hornetq</javaee:param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>HornetQServlet</servlet-name>
    <url-pattern>/HornetQServlet</url-pattern>
  </servlet-mapping>
</web-app>

standalone-full.xml 的消息传递子系统:

<subsystem xmlns="urn:jboss:domain:messaging:1.1">
    <hornetq-server>
        <persistence-enabled>true</persistence-enabled>
        <journal-file-size>102400</journal-file-size>
        <journal-min-files>2</journal-min-files>

        <connectors>
            <connector name="netty-servlet" socket-binding="http">
                <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
                <param key="servlet-path" value="/messaging/HornetQServlet"/>
                <param key="host" value="localhost"/>
                <param key="use-servlet" value="true"/>
                <param key="http-enabled" value="true"/>
                <param key="port" value="8080"/>
            </connector>
            <netty-connector name="netty" socket-binding="messaging"/>
            <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                <param key="batch-delay" value="50"/>
            </netty-connector>
            <in-vm-connector name="in-vm" server-id="0"/>
        </connectors>

        <acceptors>
            <acceptor name="netty-servlet">
                <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>
                <param key="use-invm" value="true"/>
                <param key="host" value="org.hornetq"/>
            </acceptor>
            <acceptor name="netty-invm">
                <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>
                <param key="use-invm" value="true"/>
                <param key="host" value="org.hornetq"/>
            </acceptor>
            <netty-acceptor name="netty" socket-binding="messaging"/>
            <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                <param key="batch-delay" value="50"/>
                <param key="direct-deliver" value="false"/>
            </netty-acceptor>
            <in-vm-acceptor name="in-vm" server-id="0"/>
        </acceptors>

        <security-settings>
            <security-setting match="#">
                <permission type="send" roles="guest"/>
                <permission type="consume" roles="guest"/>
                <permission type="createNonDurableQueue" roles="guest"/>
                <permission type="deleteNonDurableQueue" roles="guest"/>
            </security-setting>
        </security-settings>

        <address-settings>
            <address-setting match="#">
                <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                <redelivery-delay>0</redelivery-delay>
                <max-size-bytes>10485760</max-size-bytes>
                <address-full-policy>BLOCK</address-full-policy>
                <message-counter-history-day-limit>10</message-counter-history-day-limit>
            </address-setting>
        </address-settings>

        <jms-connection-factories>
            <connection-factory name="ServletConnectionFactory">
                <connectors>
                    <connector-ref connector-name="netty-servlet"/>
                </connectors>
                <entries>
                    <entry name="ServletConnectionFactory"/>
                    <entry name="java:jboss/exported/jms/ServletConnectionFactory"/>
                </entries>
            </connection-factory>
            <connection-factory name="InVmConnectionFactory">
                <connectors>
                    <connector-ref connector-name="in-vm"/>
                </connectors>
                <entries>
                    <entry name="java:/ConnectionFactory"/>
                </entries>
            </connection-factory>
            <connection-factory name="RemoteConnectionFactory">
                <connectors>
                    <connector-ref connector-name="netty"/>
                </connectors>
                <entries>
                    <entry name="RemoteConnectionFactory"/>
                    <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                </entries>
            </connection-factory>
            <pooled-connection-factory name="hornetq-ra">
                <transaction mode="xa"/>
                <connectors>
                    <connector-ref connector-name="in-vm"/>
                </connectors>
                <entries>
                    <entry name="java:/JmsXA"/>
                </entries>
            </pooled-connection-factory>
        </jms-connection-factories>

        <jms-destinations>
            <jms-queue name="testQueue">
                <entry name="/queue/testQueue"/>
                <entry name="java:jboss/exported/jms/queue/testQueue"/>
            </jms-queue>
        </jms-destinations>
    </hornetq-server>
</subsystem>

堆栈跟踪:

13:31:29,747 WARN [org.jboss.netty.channel.socket.http.HttpTunnelingServlet] (http--127.0.0.1-8080-1) 端点不可用:连接被拒绝:java.net.ConnectException:连接被拒绝 在 org.jboss.netty.channel.local.LocalClientChannelSink.connect(LocalClientChannelSink.java:113) [netty-3.2.6.Final.jar:] 在 org.jboss.netty.channel.local.LocalClientChannelSink.eventSunk(LocalClientChannelSink.java:73) [netty-3.2.6.Final.jar:] 在 org.jboss.netty.channel.Channels.connect(Channels.java:541) [netty-3.2.6.Final.jar:] 在 org.jboss.netty.channel.AbstractChannel.connect(AbstractChannel.java:210) [netty-3.2.6.Final.jar:] 在 org.jboss.netty.channel.socket.http.HttpTunnelingServlet.service(HttpTunnelingServlet.java:150) [netty-3.2.6.Final.jar:] 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final] 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:] 在 org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final] 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:] 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:] 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:] 在 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:] 在 org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:] 在 java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_22]

【问题讨论】:

【参考方案1】:

您可能已经解决了您的问题,但如果没有,我遇到了完全相同的错误,但在添加接受器定义后设法解决了它;即您在上面定义的 netty-invm。

        <connectors>
            <netty-connector name="netty" socket-binding="messaging"/>
            <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                <param key="batch-delay" value="50"/>
            </netty-connector>
            <netty-connector name="netty-servlet" socket-binding="http">
                <param key="host" value="$jboss.bind.address"/>
                <param key="use-servlet" value="true"/>
                <param key="servlet-path" value="/hornetq-servlet/HornetQServlet"/>
            </netty-connector>
            <in-vm-connector name="in-vm" server-id="0"/>
        </connectors>

        <acceptors>
            <netty-acceptor name="netty" socket-binding="messaging"/>
            <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                <param key="batch-delay" value="50"/>
                <param key="direct-deliver" value="false"/>
            </netty-acceptor>
                <acceptor name="netty-invm">
                    <factory-class>
                      org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory
                    </factory-class>
                    <param key="use-invm" value="true"/>
                    <param key="host" value="org.hornetq"/>
                </acceptor>
                <in-vm-acceptor name="in-vm" server-id="0"/>
        </acceptors>

我认为您不需要启用 http 的属性设置,而且您只需要一个将消息转发到 in-vm 连接器的接受器,因此 netty-servlet 接受器可以去。

我遇到的下一个问题是发送消息时出现以下异常

2012-08-06 18:09:54,557 ERROR [org.apache.catalina.connector.CoyoteAdapter](http-executor-threads - 2) An exception or error occurred in the container during the request processing: java.lang.IllegalStateException: JBAS018053: No security context found
    at org.jboss.as.web.security.SecurityActions$6.run(SecurityActions.java:136)
    at org.jboss.as.web.security.SecurityActions$6.run(SecurityActions.java:130)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.jboss.as.web.security.SecurityActions.popRunAsIdentity(SecurityActions.java:130)
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:155)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:518)
    at org.jboss.threads.SimpleDirectExecutor.execute(SimpleDirectExecutor.java:33)
    at org.jboss.threads.QueueExecutor.runTask(QueueExecutor.java:801)
    at org.jboss.threads.QueueExecutor.access$100(QueueExecutor.java:45)
    at org.jboss.threads.QueueExecutor$Worker.run(QueueExecutor.java:821)
    at java.lang.Thread.run(Thread.java:662)
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)

我正在追这个帖子的下一部分https://community.jboss.org/message/752593

【讨论】:

【参考方案2】:

试试类似的东西

ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
Queue q = (Queue) ic.lookup("/queue/testQueue");

我不知道什么类型的 jboss 用户是“管理员”,它应该是应用程序用户。 不要忘记包含 jboss jar。

【讨论】:

【参考方案3】:

这对我有用。

带有接受器的服务器

        <subsystem xmlns="urn:jboss:domain:messaging:1.3">
            <hornetq-server>
                <persistence-enabled>true</persistence-enabled>

                <cluster-user>jms-user</cluster-user>
                <cluster-password>12345678</cluster-password>

                <connectors>
                    <netty-connector name="netty" socket-binding="messaging" />
                    <in-vm-connector name="in-vm" server-id="0" />
                </connectors>

                <acceptors>
                    <netty-acceptor name="netty" socket-binding="messaging" />
                    <in-vm-acceptor name="in-vm" server-id="0" />

                    <acceptor name="netty-servlet">
                        <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class>
                        <param key="use-invm" value="true"/>
                        <param key="host" value="org.hornetq"/>
                    </acceptor>

                </acceptors>

                <security-settings>
                    <security-setting match="#">
                        <permission type="send" roles="queueSend" />
                        <permission type="consume" roles="queueConsume" />
                        <permission type="createNonDurableQueue" roles="queueMngNonDurable" />
                        <permission type="deleteNonDurableQueue" roles="queueMngNonDurable" />
                    </security-setting>
                </security-settings>

                <address-settings>
                    <address-setting match="#">
                        <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                        <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                        <redelivery-delay>1000</redelivery-delay>
                        <max-delivery-attempts>3</max-delivery-attempts>
                        <redistribution-delay>1000</redistribution-delay>
                        <message-counter-history-day-limit>30</message-counter-history-day-limit>
                    </address-setting>
                </address-settings>

                <jms-connection-factories>
                    <pooled-connection-factory name="hornetq-ra">
                        <transaction mode="xa" />
                        <connectors>
                            <connector-ref connector-name="in-vm" />
                        </connectors>
                        <entries>
                            <entry name="java:/JmsXA" />
                            <entry name="java:/ConnectionFactory" />
                        </entries>

                        <!-- Cluster credential -->
                        <user>jms-user</user>
                        <password>12345678</password>
                    </pooled-connection-factory>

                </jms-connection-factories>

                <jms-destinations>

                    <jms-topic name="topic/sample_update_data">
                        <entry name="topic/sample_update_data" />
                        <entry name="jboss/exported/topic/sample_update_data" />
                    </jms-topic>

                </jms-destinations>
            </hornetq-server>
        </subsystem>

带有连接器的服务器

    <subsystem xmlns="urn:jboss:domain:messaging:1.3">
        <hornetq-server>
            <persistence-enabled>true</persistence-enabled>

            <connectors>
                <netty-connector name="netty" socket-binding="messaging"/>
                <netty-connector name="netty-servlet" socket-binding="external-messaging">
                    <param key="use-servlet" value="true"/>
                    <param key="servlet-path" value="/mcc-hornetq/HornetQServlet"/>
                </netty-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
            </connectors>

            <acceptors>
                <netty-acceptor name="netty" socket-binding="messaging"/>
                <in-vm-acceptor name="in-vm" server-id="0"/>
            </acceptors>

            <security-settings>
                <security-setting match="#">
                    <permission type="send" roles="queueSend"/>
                    <permission type="consume" roles="queueConsume"/>
                    <permission type="createNonDurableQueue" roles="queueMngNonDurable"/>
                    <permission type="deleteNonDurableQueue" roles="queueMngNonDurable"/>
                </security-setting>
            </security-settings>

            <address-settings>
                <address-setting match="#">
                    <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                    <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                    <redelivery-delay>30000</redelivery-delay>
                    <max-delivery-attempts>1</max-delivery-attempts>
                    <message-counter-history-day-limit>30</message-counter-history-day-limit>
                </address-setting>
            </address-settings>

            <jms-connection-factories>
                <pooled-connection-factory name="hornetq-ra">
                    <transaction mode="xa"/>
                    <connectors>
                        <connector-ref connector-name="in-vm"/>
                    </connectors>
                    <entries>
                        <entry name="java:/JmsXA"/>
                        <entry name="java:/ConnectionFactory"/>
                    </entries>
                </pooled-connection-factory>
                <pooled-connection-factory name="external-hornetq-ra">
                    <transaction mode="xa"/>
                    <user>jms-user</user>
                    <password>12345678</password>
                    <connectors>
                        <connector-ref connector-name="netty-servlet"/>
                    </connectors>
                    <entries>
                        <entry name="java:/JmsXA_External"/>
                    </entries>
                    <client-failure-check-period>5000</client-failure-check-period>
                    <retry-interval>2000</retry-interval>
                    <retry-interval-multiplier>1.5</retry-interval-multiplier>
                    <max-retry-interval>60000</max-retry-interval>
                    <reconnect-attempts>10</reconnect-attempts>
                </pooled-connection-factory>
            </jms-connection-factories>

            <jms-destinations>
                <jms-queue name="queue/another_data">
                    <entry name="queue/another_data"/>
                </jms-queue>
            </jms-destinations>
        </hornetq-server>
    </subsystem>


    ....


    <outbound-socket-binding name="external-messaging">
        <remote-destination host="192.168.2.58" port="8080" />
    </outbound-socket-binding>

以及外部服务器上的 MDB

@MessageDriven(activationConfig =  
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/sample_data"),
        @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "update-data"),
        @ActivationConfigProperty(propertyName = "hA", propertyValue =  "true"),
        @ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue = "true"),     
    )
@ResourceAdapter(value = "external-hornetq-ra")
public class UpdateCacheMDB implements MessageListener 

    @Override
    public void onMessage(Message message) 
        ObjectMessage objMsg = (ObjectMessage) message;
    


【讨论】:

【参考方案4】:

以上答案中的所有配置都是正确的,但是,您还需要添加所需的 Netty 依赖项,如下所示:

MANIFEST.MF

Dependencies: org.jboss.netty

jboss-deployement-structure.xml

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.jboss.netty" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

希望对大家有所帮助。

【讨论】:

以上是关于JBoss AS 7 上的 HttpTunnelingServlet的主要内容,如果未能解决你的问题,请参考以下文章

JBoss AS7 上的 AWS Java 开发工具包 - module.xml 条目

EJB远程客户端从JBoss AS 7.1迁移到Wildfly 8.1

EJB 远程客户端从 JBoss AS 7.1 迁移到 Wildfly 8.1

如何关闭 JBoss AS 7 服务器?

从 JBoss AS 7.1 迁移到 JBoss EAP 7.3.0

JBoss AS 7远程独立客户端身份验证