JMS(点对点point-to-point)

需求:将用户登录信息(IP,用户名,密码,登录时间)传递到另一项目。


pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>RegistAndLogin</groupId>
	<artifactId>RegistAndLogin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring-version>4.2.4.RELEASE</spring-version>
	</properties>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.8</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.7.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.7.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-json</artifactId>
			<version>1.19</version>
		</dependency>
	</dependencies>
</project>


applicationContext.xml文件(不知为何PooledConnectionFactory类老是加载失败,所以我用的target~)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

	<context:component-scan base-package="service"></context:component-scan>
	<context:component-scan base-package="dao"></context:component-scan>
	<context:component-scan base-package="aspect"></context:component-scan>
	
	<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="728972"></property>
	</bean>
	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://127.0.0.1:61616" /><!-- "接收者"IP -->
	</bean>

	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
																								<!--关闭 -->
	 <!-- <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
		<property name="connectionFactory" ref="targetConnectionFactory" />
		<property name="maxConnections" value="10" />
		<property name="expiryTimeout" value="10"/> 传输完关闭
	</bean> -->
	<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<property name="connectionFactory" ref="targetConnectionFactory" />
		<property name="defaultDestination" ref="queueDestination"/>
	</bean>
	
    <!--这个是队列目的地,点对点的-->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg>
            <value>guo</value><!-- "发送者"名称 -->
        </constructor-arg>
    </bean>
    <!--这个是主题目的地,一对多的-->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic01"/>
    </bean>
</beans>
    

JMSController.java文件(producer)

package controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.activemq.command.ActiveMQQueue;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import domain.User;
import service.StuManager;
import utils.GetIP;

@Controller
public class JMSController {
	@Autowired(required=true)
	private StuManager stuManager;
	@Resource(name="targetConnectionFactory")
	private ConnectionFactory factory;
	@Resource(name="queueDestination")
	private ActiveMQQueue queue;
	
	private String name;
	private String pswd;
	private String ip;
	private SimpleDateFormat df;
	private String timestamp;//时间戳(登录时间)
	
	@RequestMapping("/login")
	public String login(String xh,String psd,HttpServletRequest request) throws Exception{
		User u = stuManager.login(xh, psd);
		Connection con = factory.createConnection();
		con.start();
		Session sessionJMS = con.createSession(true, Session.CLIENT_ACKNOWLEDGE);
		MessageProducer producer = sessionJMS.createProducer(queue);
		TextMessage message = sessionJMS.createTextMessage();
		
		ip = GetIP.getIP(request);
		name = xh;
		pswd = psd;
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		timestamp = df.format(new Date());
		
		JSONObject json = new JSONObject();
		json.put("ip", ip);
		json.put("name", name);
		json.put("pswd", pswd);
		json.put("timestamp", timestamp);
		System.out.println(json.toString());
		message.setText(json.toString());
		producer.send(message);
		
		sessionJMS.commit();
		producer.close();
		sessionJMS.close();
		con.close();
		if(u != null){
			request.getSession().setAttribute("u", u);
			return "WEB-INF/jsp/loginSuc";
		}else{
			return "login";
		}
	}
}

ConsumerTest1.java文件

package test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class ConsumerTest1 {

	public static void main(String[] args) throws Exception{
		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Destination queue=new ActiveMQQueue("guo");
		//Destination topic=new ActiveMQTopic("topic");
		
		Connection con= factory.createConnection();
		con.start();
		
		Session sen=con.createSession(true, Session.AUTO_ACKNOWLEDGE);
		
		MessageConsumer consumer=sen.createConsumer(queue);
		TextMessage msg1=(TextMessage)consumer.receive();
		System.out.println(msg1.getText());
//		int m=1/0;
//		TextMessage msg2=(TextMessage)consumer.receive();
		//msg.acknowledge();
		sen.commit();
		
		
		consumer.close();
		sen.close();
		con.close();
		

	}

}



你可能感兴趣的:(JMS(点对点point-to-point))