1 weblogic的设置都在附件里面,以图片的形式展示。
?
?
2 spring 的配置文件:
?
?<!-- JNDI 配置 -->?
?? <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
????? <property name="environment">
?????? <props>
??????? <prop key="java.naming.factory.initial">
???????? weblogic.jndi.WLInitialContextFactory
??????? </prop>
??????? <prop key="java.naming.provider.url">
???????? t3://localhost:7001
??????? </prop>
??????? <prop key="java.naming.factory.url.pkgs">
????????? weblogic.jndi.factories
??????? </prop>
?????? </props>
??? ???? </property>
???? </bean>?
?
?? <!-- jms 连接工厂?ConnectionFactory 是在第11页图片里面要填写的jndi的名称-->??
??? <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
????????? <property name="jndiName" value="ConnectionFactory" />
????????? <property name="jndiTemplate" ref="jndiTemplate"/>?
???? </bean>?
<!-- jms?队列?Queue 是在第11页图片里面要填写的jndi的名称-->??
<bean id="jmsDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
?????? <property name="jndiName" value="Queue" />
?????? <property name="jndiTemplate" ref="jndiTemplate"/>
????? </bean>
<!-- jms模板-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
????????? <property name="connectionFactory" ref="jmsConnectionFactory" />
????????? <property name="defaultDestination" ref="jmsDestination" />
???</bean>
<!-- 定义发送者-->
<bean id="sender" class="com.Sender">
???? <property name="jmsTemplate" ref="jmsTemplate" />?
?</bean>
<!-- 定义接收者-->
<bean id="receiver" class="com.Receiver">
?</bean>
<!--接收者的监听器 -->
<bean id="maillistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
???????? <property name="connectionFactory" ref="jmsConnectionFactory" />
???????? <property name="destination" ref="jmsDestination" />
???????? <property name="messageListener" ref="receiver" />
???????? ?<property name="concurrentConsumers" value= "30"/>
????? <property name="autoStartup" value="true"/>
????? <property name="sessionTransacted" value="true"/>?????????
??</bean>
3 Sender.java类
//SimpleMimeObject 是自己定义的一个类
public class Sender{
?private JmsTemplate jmsTemplate;?
??? public? void sendMessage(final SimpleMimeObject message) {
??????????????? this.jmsTemplate.send(
????????????????? new MessageCreator() {
????????????????????? public Message createMessage(Session session) throws JMSException {
????????????????????????? return session.createObjectMessage(message);//message 发送的消息
????????????????????? }
????????????????? }
????????? );
????? }
?public JmsTemplate getJmsTemplate() {
??return jmsTemplate;
?}
?public void setJmsTemplate(JmsTemplate jmsTemplate) {
??this.jmsTemplate = jmsTemplate;
?}???
}
4 Receiver.java
?
public class Receiver implements MessageListener {
?public void onMessage(Message message) {
?if(message instanceof ObjectMessage) {
???? Object object = ((ObjectMessage)message).getObject();
??????????????? if(object instanceof SimpleMimeObject) {
?????????????????????????????? //接收者对消息进行处理
????????????????????????????? }
?
????????????????????????? }
??????????? }
?
}
?
?
总结:sender 发送一个消息给jms服务器,sender就不用在管这个消息了,消息会被加到jms的队列里面。
???????? receiver 在从队列里面接收消息,然后对这个消息进行处理。
new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(message);//message 发送的消息
}
}
这里报空指针是什么原因哦