香锅之activemq(不爽), ibatis和timer

上回说到menifest.mf,这回说几个重头的。从轻的说起:

1、ibatis,这个最轻,几乎不用说什么,只是按照上篇的方法,在runtime中把需要的jar加进去就成了,下图就是例子:


 


 datasource.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<sqlMap resource="mysqlmap.xml" />
</sqlMapConfig>

 

spring config配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="mysqlPersistance" class="com.monitor.bundle.persistance.mysql.MySqlPersistanceImpl">
		<property name="dataSource" ref="datasource" />
		<property name="sqlMapClient" ref="sqlMapClient" />
	</bean>
	
	<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost/monitor?characterEncoding=GB2312" />
		<property name="username" value="user" />
		<property name="password" value="XXX" />
	</bean>
	 
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:datasourceconfig.xml" />
	</bean>
	
	<osgi:service id="mysqlPersistanceService" interface="com.monitor.bundle.interfaces.PersistanceIF" ref="mysqlPersistance" />
</beans>
 

不过这点我倒是有个疑问:以前我是将连接池什么都写在datasource.xml中的,自己创建SqlMapClient,自打老四来了,就把datasource.xml简化了,都在spring中配置了,而且连接池也是在spring配置。

和老四争论未果,也不知道到底哪个好?

 

2、再说timer,可喜的是,Spring DM和timer基本上是两码事,这使得这两个东西可以在一起调用,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="timer" class="com.zeromonitor.bundle.timer.TimerController">
		<property name="probe">
			<osgi:reference interface="com.zeromonitor.bundle.interfaces.ProbeIF" />
		</property>
	</bean>

	<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="timerTask" ref="timer" />
		<property name="delay" value="0" />
		<property name="period" value="10000" />
	</bean>
	
	<bean class="org.springframework.scheduling.timer.TimerFactoryBean">  
    	<property name="scheduledTimerTasks">  
   			<list>    
   				<ref bean="scheduledTask"/>                          
   			</list>    
   		</property>  
   </bean>   
</beans>

 

3、最后剩下的就是麻烦的active mq了,以前用他的时候,特好用:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xmlns:jms="http://www.springframework.org/schema/jms"
       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
            http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
            http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">

    <!--  lets create an embedded ActiveMQ Broker -->

    <amq:broker useJmx="false" persistent="false">
        <amq:transportConnectors>
            <amq:transportConnector uri="tcp://localhost:61234"/>
        </amq:transportConnectors>
    </amq:broker>

    

    <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
    <amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61234"/>

    <!-- a sample POJO consumer -->
    <bean id="receiver" class="activemq.SpringReceiver"/>

    
    <jms:listener-container connection-factory="jmsFactory" destination-type="queue" concurrency="10" >
        <!-- 这里的destination是  physicalName -->
        <jms:listener destination="org.apache.activemq.command.ActiveMQQueue" ref="receiver" method="onMessage"/>
    </jms:listener-container>


</beans>

 

用amp和jms的namespace就都搞定了,又能自动起amq,又能监听。

但是用Spring DM以后一切都完蛋了!

如果按照如上的配置,报一个不能refresh的错误(具体的忘了),查了springsource的jira以后,发现这个问题是无法解决的,原因是:broker和监听随着spring一起来,就不能更改了,你DM一会儿start,一会儿uninstall的,没法儿正确创建服务和监听。

所以,现在闹得我们自己编代码监听,而且在程序外部起amp。

谁要知道怎么解决,一定赐教一把啊

你可能感兴趣的:(spring,bean,ibatis,activemq,osgi)