jca 1.5 inbound 学习笔记 (by quqi99)

jca inbound 学习笔记 (by quqi99) 


作者:张华 发表于:2009-10-14
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。

http://blog.csdn.net/quqi99

 

看了下jca 1.5  inbound, 做个笔记,直接上代码吧。

 package inbound;
import inbound.eis.Location;
import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.BootstrapContext;
import javax.resource.spi.UnavailableException;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.xa.XAResource;
/**
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
class TradeResourceAdapter implements javax.resource.spi.ResourceAdapter {
    private BootstrapContext ctx;
    private TradeWorker worker;

    public void start(BootstrapContext ctx) {
        this.ctx = ctx;
    }

    public void stop() {
        worker.release();
    }

    public void endpointActivation(MessageEndpointFactory endpointFactory,ActivationSpec spec) throws ResourceException {
        try {
            TradeActivationSpec tradeActivationSpec = (TradeActivationSpec)spec;
            // Create a message endpoint from the endpoint factory. The disadvantage of doing so is that
            // you end up with just one instance of MDB and hence not taking the advantage of object pooling. See the text for more information.
            MessageEndpoint mep = endpointFactory.createEndpoint(null);
            if (mep instanceof TradeMessageListener) {
                // Use spec to fetch Connection Factory from JNDI.
                TradeConnectionFactory f = null;          // Lookup JNDI.
                f = new TradeConnectionFactory();         //test
                // Use spec to fetch Location
                Location location = null;                  // Lookup JNDI.
                location = new Location();
                location.setMarket(tradeActivationSpec.get_location());
                // Use Connection Factory to create Connection.
                TradeConnection connection = f.createConnection(location);
                worker = new TradeWorker(spec, endpointFactory, connection);

                // Scheduling the work.
                ctx.getWorkManager().scheduleWork(worker);
            }
        } catch (UnavailableException e) {

        }
    }

    public XAResource[] getXAResources(ActivationSpec[] activationSpecs) {
        return null;
    }

    public void endpointDeactivation(MessageEndpointFactory arg0,
            ActivationSpec arg1) {
    }

}

 

 

package inbound;

import inbound.eis.TradeMessage;

import javax.resource.spi.ActivationSpec;
import javax.resource.spi.UnavailableException;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
public class TradeWorker implements javax.resource.spi.work.Work {
    private ActivationSpec _spec;
    private MessageEndpoint _mep;
    private MessageEndpointFactory _mef;
    private TradeConnection _conn;
    private boolean ok = true;

    public TradeWorker(ActivationSpec spec, MessageEndpointFactory _mef,TradeConnection con) {
        // Hold these references for this Work instance
        _spec = spec;
        _mef = _mef;
        _conn = con;
    }

    public void run() {
        while (ok) {
            // Connection receives the trade messages
            TradeMessage msg = _conn.receive();
            // Invoke the message endpoint(MDB)'s onMessage
            // for processing a trade message
            if (msg != null) {
                try {
                    _mep = _mef.createEndpoint(null);
                    if (_mep instanceof TradeMessageListener) {
                        ((TradeMessageListener) _mep).onMessage(msg);
                    }
                } catch (UnavailableException e) {
                    e.printStackTrace();
                }
               
            }
        }
    }

    public void release() {
    }
}

 

 

package inbound;

import inbound.eis.TradeMessage;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
public interface TradeMessageListener {
       public void   onMessage(TradeMessage message);
    }

 

 

package inbound;

import inbound.eis.Location;

import javax.naming.NamingException;
import javax.naming.Reference;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
class TradeConnectionFactory implements javax.resource.cci.ConnectionFactory {
    private static final long serialVersionUID = 1L;

    public TradeConnection createConnection(Location location){
           return new TradeConnection(location);
       }

    public Connection getConnection() throws ResourceException {
        return null;
    }

    public Connection getConnection(ConnectionSpec arg0)
            throws ResourceException {
        return null;
    }

    public ResourceAdapterMetaData getMetaData() throws ResourceException {
        return null;
    }

    public RecordFactory getRecordFactory() throws ResourceException {
        return null;
    }

    public void setReference(Reference arg0) {
    }

    public Reference getReference() throws NamingException {
        return null;
    }
    }

 

package inbound;

import inbound.eis.Location;
import inbound.eis.TradeMessage;

import javax.resource.ResourceException;
import javax.resource.cci.ConnectionMetaData;
import javax.resource.cci.Interaction;
import javax.resource.cci.LocalTransaction;
import javax.resource.cci.ResultSetInfo;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
class TradeConnection implements javax.resource.cci.Connection {
    private Location location;

    public TradeConnection(Location location) {
        this.location = location;
    }

    public TradeMessage receive() {
        TradeMessage message = new TradeMessage();
        message.setMarket(location.getMarket());
        if (location.getMarket().equals("bj"))
            message.setStr("bj业绩良好");
        else
            message.setStr("bj以外业绩不好");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return message;
    }

    public void close() throws ResourceException {
    }

    public Interaction createInteraction() throws ResourceException {
        return null;
    }

    public LocalTransaction getLocalTransaction() throws ResourceException {
        return null;
    }

    public ConnectionMetaData getMetaData() throws ResourceException {
        return null;
    }

    public ResultSetInfo getResultSetInfo() throws ResourceException {
        return null;
    }
}

 

package inbound;

import javax.resource.ResourceException;
import javax.resource.spi.InvalidPropertyException;
import javax.resource.spi.ResourceAdapter;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
class TradeActivationSpec implements javax.resource.spi.ActivationSpec {
    private String _factory;
    private String _location;
    private ResourceAdapter _adapter;

    public String getFactory() {
        return _factory;
    }

    public void validate() throws InvalidPropertyException {
    }

    public ResourceAdapter getResourceAdapter() {
        return null;
    }

    public void setResourceAdapter(ResourceAdapter arg0)
            throws ResourceException {
    }

    public String get_factory() {
        return _factory;
    }

    public void set_factory(String _factory) {
        this._factory = _factory;
    }

    public String get_location() {
        return _location;
    }

    public void set_location(String _location) {
        this._location = _location;
    }

    public ResourceAdapter get_adapter() {
        return _adapter;
    }

    public void set_adapter(ResourceAdapter _adapter) {
        this._adapter = _adapter;
    }

   
}

 

package inbound.eis;
/**
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
public class TradeMessage implements java.io.Serializable {
    /**  */
    private static final long serialVersionUID = 1L;
    public String market;
    public String str;
    public String getMarket() {
        return market;
    }
    public void setMarket(String market) {
        this.market = market;
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }   
}

 

 

package inbound.eis;
/**
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
public class Location implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    public String market;

    public String getMarket() {
        return market;
    }
    public void setMarket(String market) {
        this.market = market;
    }
}

 

ra.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<connector id="Connector_ID" version="1.5" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd">
    <display-name>Market Trade Resource Adapter</display-name>
    <vendor-name>zhanghua</vendor-name>
    <eis-type>Trade</eis-type>
    <resourceadapter-version>1.0</resourceadapter-version>
    <resourceadapter>
        <resourceadapter-class>TradeResourceAdapter
        </resourceadapter-class>
        <inbound-resourceadapter>
            <messageadapter>
                <messagelistener>
                    <messagelistener-type>TradeMessageListener</messagelistener-type>
                    <activationspec>
                        <activationspec-class>TradeActivationSpec</activationspec-class>
                        <required-config-property>
                            <config-property-name>Factory</config-property-name>
                        </required-config-property>
                        <required-config-property>
                            <config-property-name>Location</config-property-name>
                        </required-config-property>
                    </activationspec>
                </messagelistener>
            </messageadapter>
        </inbound-resourceadapter>
    </resourceadapter>
</connector>

 

 

EJB为:

package jca;

import inbound.TradeMessageListener;
import inbound.eis.TradeMessage;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;

/**
 *
 *
 * @version 0.10 2009-10-14
 * @author Zhang Hua
 */
//@MessageDriven(name = "TradeReceiverBean", messageListenerInterface = TradeMessageListener.class, activationConfig = {
//        @ActivationConfigProperty(propertyName = "factory", propertyValue = "mytrader/ConnectionFactory"),
//        @ActivationConfigProperty(propertyName = "market", propertyValue = "bj") })
public class TradeReceiverBean implements MessageDrivenBean,
        TradeMessageListener {

    private static final long serialVersionUID = 1L;

    public void onMessage(TradeMessage message) {
        System.out.println("message driver bean");
    }

    public void onMessage(Message msg) {

    }

    public void ejbRemove() throws EJBException {
    }

    public void setMessageDrivenContext(MessageDrivenContext arg0)
            throws EJBException {
    }

}

 

ejb-jar.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
    version="3.0">
    <enterprise-beans>
        <message-driven>
            <ejb-name>TradeReceiverBean</ejb-name>
            <ejb-class>jca.TradeReceiverBean</ejb-class>
            <messaging-type>TradeMessageListener</messaging-type>
            <transaction-type>Container</transaction-type>
            <activation-config>
                <activation-config-property>
                    <activation-config-property-name>factory</activation-config-property-name>
                    <activation-config-property-value>mytrader/ConnectionFactory</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>market</activation-config-property-name>
                    <activation-config-property-value>bj</activation-config-property-value>
                </activation-config-property>
            </activation-config>
        </message-driven>
    </enterprise-beans>
</ejb-jar>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quqi99

你的鼓励就是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值