第2.1.6章 WEB系统最佳实践Spring文件配置之spring-hessian-client.xml

本文介绍了一个使用Hessian进行服务端调用的例子,展示了如何通过Spring配置文件及编程方式实现远程服务调用。文章包括了具体的配置示例和服务调用代码。

spring-hessian-client.xml是hessian客户端调用的配置,可以配置多个.服务端的配置参考spring-hessian-server.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-lazy-init="false">
    <description>Spring hessian配置</description>
    <!-- 同步传感器数据 -->
    <bean id="syncMonitorAutoCollectDataService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorAutoCollectData</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorAutoCollectDataService</value>
        </property>
    </bean>
    <!-- 同步运行日志 -->
    <bean id="syncMonitorRunLogService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorRunLog</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorRunLogService</value>
        </property>
    </bean>
    <!-- 同步维修日志 -->
    <bean id="syncMonitorMaintainLogService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorMaintainLog</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorMaintainLogService</value>
        </property>
    </bean>
    <!-- 同步入库表 -->
    <bean id="syncMonitorInstockService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorInstock</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorInstockService</value>
        </property>
    </bean>
    <!-- 同步出库表 -->
    <bean id="syncMonitorOutstockService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorOutstock</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorOutstockService</value>
        </property>
    </bean>
    <!-- 同步轮衬表单-->
    <bean id="syncMonitorWheelBillService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorWheelBill</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorWheelBillService</value>
        </property>
    </bean>
    <!-- 同步钢绳测量表单 -->
    <bean id="syncMonitorWireBillService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorWireBill</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorWireBillService</value>
        </property>
    </bean>
    <!-- 同步停车记录 -->
    <bean id="syncMonitorStopCarService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8097/monitor/hessian/syncMonitorStopCar</value>
        </property>
        <property name="serviceInterface">
            <value>com.dzmsoft.monitor.api.service.SyncMonitorStopCarService</value>
        </property>
    </bean>
</beans>

很多时候,服务端地址是变得,那么我们就不能总变spring-hessian-client.xml吧,除非你用disconf来管理配置文件,但是那为啥又不直接使用dubbo呢?除了配置的方式,另外一种方式,就可以写代码调用,下面是实例

@Transactional(readOnly = false)
    @Override
    public void sync() {
        List<FontalInstock> fontalInstocks = fontalInstockService.selectNoSyncDatas();
        if (!CheckEmptyUtil.isEmpty(fontalInstocks)){
            String datas = gson.toJson(fontalInstocks);
            String merchCode = fontalConfigService.getMerchCode();
            String url = fontalConfigService.getMonitorUrl() + WorkerEnum.syncMonitorInstock.value();
            String upDown = fontalConfigService.getUpDown();
            //
            HessianProxyFactory factory  = new HessianProxyFactory();
            SyncMonitorInstockService syncMonitorInstockService;
            String result = null;
            try {
                syncMonitorInstockService = (SyncMonitorInstockService)factory.create(SyncMonitorInstockService.class,url);
                result = syncMonitorInstockService.sync(datas, merchCode, upDown);
                if (!CheckEmptyUtil.isEmpty(result)){
                    // 更改上传结果
                    callback(result);
                }
            } catch (MalformedURLException e) {
                logger.error("远程调用monitor 失败,失败原因:{}",e.getMessage());
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

warrah

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值