`
dev_liu
  • 浏览: 109815 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

joram+spring集成的简单示例

    博客分类:
  • JMS
阅读更多

简单的消息发送<o:p></o:p>

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

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

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

<o:p> </o:p>

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

<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>

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

<!------配置文件内容-------><o:p></o:p>

<beans><o:p></o:p>

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

<bean id="connectionFactory" <o:p></o:p>

class="org.springframework.jndi.JndiObjectFactoryBean"> <o:p></o:p>

<property name="jndiName"> <o:p></o:p>

<value>qcf</value> <o:p></o:p>

</property> <o:p></o:p>

<property name="jndiTemplate"> <o:p></o:p>

<ref local="jndiTemplate"></ref> <o:p></o:p>

</property> <o:p></o:p>

</bean> <o:p></o:p>

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

<bean id="destination" <o:p></o:p>

class="org.springframework.jndi.JndiObjectFactoryBean"> <o:p></o:p>

<property name="jndiName"> <o:p></o:p>

<value>queue</value> <o:p></o:p>

</property> <o:p></o:p>

<property name="jndiTemplate"> <o:p></o:p>

<ref local="jndiTemplate"></ref> <o:p></o:p>

</property> <o:p></o:p>

</bean> <o:p></o:p>

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

<o:p> </o:p>

<bean id="jmsTemplate" <o:p></o:p>

class="org.springframework.jms.core.JmsTemplate"> <o:p></o:p>

<property name="connectionFactory"> <o:p></o:p>

<ref local="connectionFactory" /> <o:p></o:p>

</property> <o:p></o:p>

<property name="defaultDestination"> <o:p></o:p>

<ref local="destination" /> <o:p></o:p>

</property> <o:p></o:p>

</bean> <o:p></o:p>

<!-----配置jndi环境-------><o:p></o:p>

<bean id="jndiTemplate" <o:p></o:p>

class="org.springframework.jndi.JndiTemplate"> <o:p></o:p>

<property name="environment"> <o:p></o:p>

<props> <o:p></o:p>

<prop key="java.naming.factory.initial"> <o:p></o:p>

fr.dyade.aaa.jndi2.client.NamingContextFactory <o:p></o:p>

</prop> <o:p></o:p>

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

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

</props> <o:p></o:p>

</property> <o:p></o:p>

</bean> <o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

</beans><o:p></o:p>

.发送消息客户端代码:<o:p></o:p>

public class JmsTemplateTest11 { <o:p></o:p>

protected static final Log log = LogFactory.getLog(JmsTemplateTest11.class); <o:p></o:p>

<o:p> </o:p>

static Context ictx = null; <o:p></o:p>

<o:p> </o:p>

public static void main(String[] args) throws NamingException, JMSException { <o:p></o:p>

<o:p> </o:p>

System.out.println(); <o:p></o:p>

System.out.println("Sends messages on the queue..."); <o:p></o:p>

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

*多个配置文件<o:p></o:p>

*/.<o:p></o:p>

分享到:
评论
1 楼 dev_liu 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...");

}
}

相关推荐

Global site tag (gtag.js) - Google Analytics