论坛首页 Java版 Spring

joram+spring集成的简单示例

浏览 1604 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-01-17 关键字: joram spring jms

简单的消息发送

.首先启动joram服务器,管理各种对象和jndi绑定(详细请看前面章节).

.在工程目录下引入spring支持的包和joram.

.在工程目录下创建applicationContext.xml文件.这个文件是spring的配置文件.示例如下

 

<!-----xml文件头,为默认,我们不需要修改---------------->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!------配置文件内容------->

<beans>

<!------连接工厂配置.注意:value值为jndi绑定后的key=”qcf”------->

<bean id="connectionFactory"

class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>qcf</value>

</property>

<property name="jndiTemplate">

<ref local="jndiTemplate"></ref>

</property>

</bean>

<!-----目的地配置,注意:queuejndi绑定后的key而不是value>

<bean id="destination"

class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>queue</value>

</property>

<property name="jndiTemplate">

<ref local="jndiTemplate"></ref>

</property>

</bean>

<!---spring-jms模版:注意:joram是支持jms1.1的所以用org.springframework.jms.core.JmsTemplate而不是用org.springframework.jms.core.JmsTemplate102------- >

 

<bean id="jmsTemplate"

class="org.springframework.jms.core.JmsTemplate">

<property name="connectionFactory">

<ref local="connectionFactory" />

</property>

<property name="defaultDestination">

<ref local="destination" />

</property>

</bean>

<!-----配置jndi环境------->

<bean id="jndiTemplate"

class="org.springframework.jndi.JndiTemplate">

<property name="environment">

<props>

<prop key="java.naming.factory.initial">

fr.dyade.aaa.jndi2.client.NamingContextFactory

</prop>

<prop key="java.naming.factory.host">localhost</prop>

<prop key="java.naming.factory.port">16400</prop>

</props>

</property>

</bean>

 

 

 

 

</beans>

.发送消息客户端代码:

public class JmsTemplateTest11 {

protected static final Log log = LogFactory.getLog(JmsTemplateTest11.class);

 

static Context ictx = null;

 

public static void main(String[] args) throws NamingException, JMSException {

 

System.out.println();

System.out.println("Sends messages on the queue...");

/**引入配置文件,推荐使用FileSystemXmlApplicationContext,这样你可以一次使用

*多个配置文件

*/.

span声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。

   
时间:2007-01-17
简单的消息发送
一.首先启动joram服务器,管理各种对象和jndi绑定(详细请看前面章节).
二.在工程目录下引入spring支持的包和joram包.
三.在工程目录下创建applicationContext.xml文件.这个文件是spring的配置文件.示例如下

<!-----xml文件头,为默认,我们不需要修改---------------->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!------配置文件内容------->
<beans>
<!------连接工厂配置.注意:value值为jndi绑定后的key=”qcf”------->
<bean id="connectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>qcf</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-----目的地配置,注意:queue为jndi绑定后的key而不是value>
<bean id="destination"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>queue</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!---spring-jms模版:注意:joram是支持jms1.1的所以用org.springframework.jms.core.JmsTemplate而不是用org.springframework.jms.core.JmsTemplate102------- >

<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory" />
</property>
<property name="defaultDestination">
<ref local="destination" />
</property>
</bean>
<!-----配置jndi环境------->
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
fr.dyade.aaa.jndi2.client.NamingContextFactory
</prop>
<prop key="java.naming.factory.host">localhost</prop>
<prop key="java.naming.factory.port">16400</prop>
</props>
</property>
</bean>




</beans>
四.发送消息客户端代码:
public class JmsTemplateTest11 {
protected static final Log log = LogFactory.getLog(JmsTemplateTest11.class);

static Context ictx = null;

public static void main(String[] args) throws NamingException, JMSException {

System.out.println();
System.out.println("Sends messages on the queue...");
/**引入配置文件,推荐使用FileSystemXmlApplicationContext,这样你可以一次使用
*多个配置文件
*/.

ApplicationContext ac = new FileSystemXmlApplicationContext(
new String[]{
"F:\\project\\SpringJMS\\src\\applicationContext.xml"});

JmsTemplate jt = (JmsTemplate) ac.getBean("jmsTemplate");
//注入bean,由于配置文件各个bean是相互关联的,所以注入jmsTemplate就注入了//其它bean,当然你也可以这样写.但是配置文件中各个bean没有关联.这段代码可以//更深入理解spring的ioc模式
//Destination de = (Destination) ac.getBean(“destination”)
//QueueConnetionfactory cf =( QueueConnectionFactory) //ac.getBean(“connectionFactory”)

/**通过jt模版发送消息,这里new MessageCreator()使用的反射原理.
*/
jt.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
// return session
// .createTextMessage("Hello World(11),queue!");
TextMessage message = (TextMessage) session.createTextMessage("bbc");//创建一个消息,内容为bbc.
return message;
}
});
log.info("成功发送消息!");
System.out.println("Success Sends messages to the queue...");

}
}
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐