首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

joram+spring集成的容易示例

2012-10-25 
joram+spring集成的简单示例简单的消息发送o:p/o:p!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN//EN

joram+spring集成的简单示例

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!------配置文件内容------->
<beans>
<!------连接工厂配置.注意:value值为jndi绑定后的key=”qcf”------->
<bean id="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...");

}
}

热点排行