directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Allen <mrchrisal...@gmail.com>
Subject Re: MINA FactoryBeans
Date Thu, 22 Dec 2005 22:09:44 GMT
Niklas,

Okay, well I have now tried using the
org.apache.mina.integration.spring.SocketConnectorFactoryBean with my Client
implementation and it doesn't work for what I'm trying to do.  Or, I quite
simply might not know what I'm doing ;-)

Anyway,  here are two methods that I use to create the client socket
connection.  The first works and doesn't use Spring to configure it.  The
second is using the technique that you described in your previous email, but
never connects, or at least never passes events to my handler.  Also note
that the way that I configure the filters in the first method has a name
value relationship that I can't seem to create using the Spring
configuration in XML.  Perhaps this is the problem?  Anyway, here's the
code:

public void connect() {
        log.info("connecting to Jabber...");
        ThreadPoolFilter ioThreadPoolFilter = new ThreadPoolFilter();
        ThreadPoolFilter protocolThreadPoolFilter = new ThreadPoolFilter();
        IoConnector connector = new SocketConnector(); //notice that I use
new here
        log.debug("connector: " + connector); //returns  connector:
org.apache.mina.transport.socket.nio.SocketConnector@530cf2
        connector.getFilterChain().addFirst("ioThreadPool",
ioThreadPoolFilter);
        connector.getFilterChain().addLast("protocolThreadPool",
                protocolThreadPoolFilter);

        connector.setConnectTimeout(connectTimeOut);

        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(
                    bridgeAccountDAO.getJabberServerHostName(),
                    bridgeAccountDAO.getJabberServerPort()),
                    (IoHandler) sessionHandler);

            session = future.getSession();
            // set the globally accessable version for other threads to
            // access:
            Scope.setJabberSession(session);
        } catch (IOException e) {
            System.err.println("Failed to connect.");
            e.printStackTrace();
        }
    }

    public void connectUsingSpring() {
        log.info("connecting to Jabber with Spring...");
        IoConnector connector = (IoConnector) ctx.getBean("socketConnector");
//I use the ApplicationContext to retrieve the bean
        log.debug("connector: " + connector); //also returns  connector:
org.apache.mina.transport.socket.nio.SocketConnector
        ConnectFuture future = connector.connect(new InetSocketAddress(
                bridgeAccountDAO.getJabberServerHostName(),
                bridgeAccountDAO.getJabberServerPort()),
                (IoHandler) sessionHandler);
        try {
            session = future.getSession();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // set the globally accessable version for other threads to
        // access:
        Scope.setJabberSession(session);
    }


Here are the MINA references in the XML config that I'm using for the
ApplicationContext:

 <bean id="sessionHandler" class="
edu.harvard.mgh.lcs.servers.bridge.JabberSessionHandlerImpl">
        <property name="bridgeAccountDAO">
            <ref local="bridgeAccountDAO"/>
        </property>
        <property name="jabbberMessageProcessor">
            <ref local="jabberMessageProcessor"/>
        </property>
        <property name="protocolCodecFactory">
            <ref local="protocolCodecFactory"/>
        </property>
    </bean>

    <bean id="socketConnector"
        class="org.apache.mina.integration.spring.SocketConnectorFactoryBean
">
        <property name="filters">
            <list>
                <ref local="threadPoolFilter"/> <!-- notice that I can't do
a mapping like it's done with Java code -->
                <ref local="protocolCodecFilter"/>
            </list>
        </property>
        <property name="connectTimeout" value="300000"/>
    </bean>

    <bean id="protocolCodecFactory" class="
edu.harvard.mgh.lcs.servers.bridge.codecfilters.JabberCodecFactory">
        <property name="charsetName" value="UTF-8"/>
    </bean>

    <bean id="protocolCodecFilter"
        class="org.apache.mina.filter.codec.ProtocolCodecFilter">
        <constructor-arg ref="protocolCodecFactory"/>
    </bean>

    <bean id="threadPoolFilter"
        class="org.apache.mina.filter.ThreadPoolFilter">
        <!-- Threads will be named IoWorker-1, IoWorker-2, etc -->
        <constructor-arg value="IoWorker"/>
        <property name="maximumPoolSize" value="10"/>
    </bean>



I'm probably missing something obvious here, so let me know if you see
anything.

Thanks,
Chris

Mime
View raw message