spring+weblogic+jms配制

1.applicationContext-jms.xml 配制内容如果下

<!--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://127.0.0.1:7001
				</prop>
				<prop key="java.naming.factory.url.pkgs">
					weblogic.jndi.factories
				</prop>
				<prop key="java.naming.security.principal">
					weblogic
				</prop>
				<prop key="java.naming.security.credentials">
					12345678
				</prop>

			</props>
		</property>
	</bean>


<!--连接工厂配制-->
<bean id="jmsConnectionFactory"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="myConnectionFactory-0" />
		<property name="jndiTemplate" ref="jndiTemplate" />
	</bean>


	<bean id="singleConnectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory102">
		<property name="targetConnectionFactory"
			ref="jmsConnectionFactory" />
		<property name="clientId" value="sp_receiver" /><!-- receiver client id -->
	</bean>

<!--消息队列-->
	<bean id="jmsQueue"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="myQueue" />
		<property name="jndiTemplate" ref="jndiTemplate" />
	</bean>

	<bean id="jmsTopic"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="mYTopic" />
		<property name="jndiTemplate" ref="jndiTemplate" />
	</bean>


<bean id="jmsReceiver" class="com.vst.test.jms.MyMessageListener" />


<!--接收端配制-->
	<bean id="listenerQueue"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
		<property name="connectionFactory" ref="singleConnectionFactory" />
		<property name="destination" ref="jmsQueue" />
		<property name="messageListener" ref="jmsReceiver" />
		<property name="autoStartup" value="true" />
	</bean>

	<bean id="listenerTopic"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
		<property name="connectionFactory" ref="singleConnectionFactory" />
		<property name="destination" ref="jmsTopic" />
		<property name="messageListener" ref="jmsReceiver" />
		<property name="pubSubDomain" value="true" />
		<property name="autoStartup" value="true" />
	</bean>

<!--发送端配制-->

<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate102">
		<property name="connectionFactory"
			ref="jmsConnectionFactory" />
		<property name="defaultDestination" ref="jmsTopic" />
		<property name="pubSubDomain" value="true" />
		<property name="explicitQosEnabled" value="true" />
	</bean>
	
	
	<bean id="jmsTopicProducter"
		class="com.vst.test.jms.TestSend">
		<property name="jmsTemplate" ref="jmsTemplate" />
	</bean>


配制文件完成,接下来看程序

发送端:

public class TestSend {
	
	private JmsTemplate102 jmsTemplate;

        public void setJmsTemplate(JmsTemplate102 jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

public void sendTopicMsg(final String msg){
		try {
			this.jmsTemplate.send(new MessageCreator() {
				
				public Message createMessage(Session session)
						throws JMSException {
					MessageObj mObj=new MessageObj();
					mObj.setMsgText(msg);
					Message message =                  session.createObjectMessage();
					((ObjectMessage) message).setObject((Serializable) mObj);
					return message;
				}

			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


接收端:

public class TopicListener implements MessageListener {
	 public void onMessage(Message message) {
		 		 	
	        try {
	        	if (message instanceof ObjectMessage) {
	        		ObjectMessage objM=(ObjectMessage)message;
	        		MessageObj mObj=(MessageObj)objM.getObject();
	        		System.out.println(mObj.getMsgText());
				}else if(message instanceof TextMessage){
					TextMessage tm=(TextMessage)message;
					System.out.println(tm.getText());
				}

	        } catch (JMSException e) {
	            e.printStackTrace();
	        }
	 
	    }

}

你可能感兴趣的:(spring,weblogic,jms)