Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xin committed Feb 8, 2015
1 parent 6223c25 commit 994d49b
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 1 deletion.
14 changes: 14 additions & 0 deletions springMQ6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand All @@ -33,6 +43,10 @@
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>




<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
Expand Down
5 changes: 4 additions & 1 deletion springMQ6/springMQ6.iml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Tomcat8" level="application_server_libraries" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jms:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:3.2.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-test:3.2.13.RELEASE" level="project" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.tiantian.springintejms.controller;

import com.tiantian.springintejms.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jms.Destination;

/**
* Created by xin on 15-2-8 下午8:46
*
* @project activeMQ
* @package com.tiantian.springintejms.controller
* @Description
* @blog http://blog.csdn.net/u011439289
* @email [email protected]
* @github https://github.com/888xin
*/

@Controller
@RequestMapping("mq")
public class SenderController {

@Autowired
private ProducerService producerService;
@Autowired
@Qualifier("queueDestination")
private Destination destination;

@Autowired
@Qualifier("topicDestination")
private Destination topicDestination ;

@RequestMapping("/send")
public void send(){
for (int i=0; i<2; i++) {
producerService.sendMessage(destination, "你好,生产者!这是消息:" + (i+1));
}
}
}
21 changes: 21 additions & 0 deletions springMQ6/src/main/resources/spring-mvc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 扫描controller(controller层注入) -->
<context:component-scan base-package="com.tiantian.springintejms.controller"/>

<!-- 对模型视图添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
27 changes: 27 additions & 0 deletions springMQ6/web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,31 @@
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<!-- 读取spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml;</param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMVC核心配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


</web-app>
Binary file added springMQ7/lib/activemq-all-5.9.0.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions springMQ7/springMQ7.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="activemq7" level="project" />
</component>
</module>

68 changes: 68 additions & 0 deletions springMQ7/src/com/lhx/activemq/Receiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.lhx.activemq;

/**
* Created by xin on 15-2-8 上午10:39
*
* @project activeMQ
* @package com.lhx.activemq
* @Description
* @blog http://blog.csdn.net/u011439289
* @email [email protected]
* @github https://github.com/888xin
*/

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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
// 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
84 changes: 84 additions & 0 deletions springMQ7/src/com/lhx/activemq/Sender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.lhx.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
* Created by xin on 15-2-8 上午10:37
*
* @project activeMQ
* @package com.lhx.activemq
* @Description
* @blog http://blog.csdn.net/u011439289
* @email [email protected]
* @github https://github.com/888xin
*/

public class Sender {

private static final int SEND_NUMBER = 5;

public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS
// Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try { // 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}


public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送的消息"
+ i);
// 发送消息到目的地方

System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}

0 comments on commit 994d49b

Please sign in to comment.