Jelajahi Sumber

增加cms-mq独立mq消费者模块

shuzheng 9 tahun lalu
induk
melakukan
be1d583313

+ 96 - 0
cms/cms-mq/pom.xml

@@ -0,0 +1,96 @@
+<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/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>com.zheng</groupId>
+        <artifactId>zheng-cms</artifactId>
+        <version>1.0.0</version>
+    </parent>
+
+    <artifactId>zheng-cms-mq</artifactId>
+    <packaging>war</packaging>
+
+    <name>zheng-cms-mq Maven Webapp</name>
+    <url>http://www.zhangshuzheng.cn</url>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.zheng</groupId>
+            <artifactId>zheng-cms-service</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mortbay.jetty</groupId>
+            <artifactId>jetty-maven-plugin</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>jstl</artifactId>
+        </dependency>
+    </dependencies>
+
+    <profiles>
+        <profile>
+            <id>dev</id>
+            <properties>
+                <env>dev</env>
+            </properties>
+            <activation>
+                <activeByDefault>true</activeByDefault>
+            </activation>
+        </profile>
+        <profile>
+            <id>test</id>
+            <properties>
+                <env>test</env>
+            </properties>
+        </profile>
+        <profile>
+            <id>prod</id>
+            <properties>
+                <env>prod</env>
+            </properties>
+        </profile>
+    </profiles>
+
+    <build>
+        <finalName>cms-mq</finalName>
+        <filters>
+            <filter>src/main/resources/profiles/${env}.properties</filter>
+        </filters>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>true</filtering>
+            </resource>
+        </resources>
+        <plugins>
+            <!-- jetty插件 -->
+            <plugin>
+                <groupId>org.mortbay.jetty</groupId>
+                <artifactId>jetty-maven-plugin</artifactId>
+                <version>8.1.9.v20130131</version>
+                <configuration>
+                    <scanIntervalSeconds>3</scanIntervalSeconds>
+                    <webApp>
+                        <contextPath>/cms-mq</contextPath>
+                    </webApp>
+                    <connectors>
+                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
+                            <port>8081</port>
+                        </connector>
+                    </connectors>
+                    <reload>automatic</reload>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 37 - 0
cms/cms-mq/src/main/java/com/zheng/cms/mq/jms/defaultQueueMessageListener.java

@@ -0,0 +1,37 @@
+package com.zheng.cms.mq.jms;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
+
+/**
+ * MQ消费者
+ * Created by ZhangShuzheng on 2016/11/24.
+ */
+public class defaultQueueMessageListener implements MessageListener {
+
+	private static Logger _log = LoggerFactory.getLogger(defaultQueueMessageListener.class);
+
+	@Autowired
+	ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
+	public void onMessage(final Message message) {
+		// 使用线程池多线程处理
+		threadPoolTaskExecutor.execute(new Runnable() {
+			public void run() {
+				TextMessage textMessage = (TextMessage) message;
+				try {
+					_log.info("cms-mq接收到:{}", textMessage.getText());
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+		});
+	}
+
+}

+ 37 - 0
cms/cms-mq/src/main/resources/applicationContext-activemq.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+	   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-3.2.xsd"
+	   default-autowire="byName">
+
+	<!-- 连接工厂 -->
+	<bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
+		<property name="brokerURL" value="${AvtiveMQ.brokerURL}"/>
+	</bean>
+	<!--<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">-->
+	<!--<property name="connectionFactory" ref="activeMqConnectionFactory"/>-->
+	<!--<property name="maxConnections" value="10"/>-->
+	<!--</bean>-->
+	<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
+		<property name="targetConnectionFactory" ref="activeMqConnectionFactory"/>
+		<property name="sessionCacheSize" value="100"/>
+	</bean>
+	<!-- 点对点队列 -->
+	<bean id="defaultQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
+		<constructor-arg index="0" value="com.zheng.cms.queue.default"/>
+	</bean>
+	<!-- 一对多队列 -->
+	<bean id="defaultTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">
+		<constructor-arg index="0" value="com.zheng.cms.topic.default"/>
+	</bean>
+
+	<!-- 消费者 -->
+	<bean id="defaultQueueMessageListener" class="com.zheng.cms.mq.jms.defaultQueueMessageListener"/>
+	<bean id="jmsContainer"  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
+		<property name="connectionFactory" ref="connectionFactory"/>
+		<property name="destination" ref="defaultQueueDestination"/>
+		<property name="messageListener" ref="defaultQueueMessageListener"/>
+	</bean>
+
+</beans>

+ 18 - 0
cms/cms-mq/src/main/resources/applicationContext-threadpool.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+	   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-3.0.xsd">
+
+	<!-- 线程池配置 -->
+	<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
+		<!-- 线程池维护线程的最少数量 -->
+		<property name="corePoolSize" value="50" />
+		<!--  线程池维护线程的最大数量,默认为Integer.MAX_VALUE -->
+		<property name="maxPoolSize" value="1000" />
+		<!-- 线程池所使用的缓冲队列,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE -->
+		<property name="queueCapacity" value="200" />
+		<!-- 线程池维护线程所允许的空闲时间,默认为60s -->
+		<property name="keepAliveSeconds" value="300" />
+	</bean>
+
+</beans>

+ 1 - 0
cms/cms-mq/src/main/resources/config.properties

@@ -0,0 +1 @@
+env=${profile.env}

+ 11 - 0
cms/cms-mq/src/main/resources/log4j.properties

@@ -0,0 +1,11 @@
+#off/fatal/error/warn/info/debug/all
+log4j.debug=false
+log4j.rootLogger=info, stdout
+
+# Console output
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
+
+#Spring logging configuration
+log4j.category.org.springframework = warn

+ 3 - 0
cms/cms-mq/src/main/resources/profiles/dev.properties

@@ -0,0 +1,3 @@
+profile.env=dev
+### activeMq
+AvtiveMQ.brokerURL=failover:(tcp://127.0.0.1:61616?wireFormat.maxInactivityDuration=0)

+ 3 - 0
cms/cms-mq/src/main/resources/profiles/prod.properties

@@ -0,0 +1,3 @@
+profile.env=prod
+### activeMq
+AvtiveMQ.brokerURL=failover:(tcp://127.0.0.1:61616?wireFormat.maxInactivityDuration=0)

+ 3 - 0
cms/cms-mq/src/main/resources/profiles/test.properties

@@ -0,0 +1,3 @@
+profile.env=test
+### activeMq
+AvtiveMQ.brokerURL=failover:(tcp://127.0.0.1:61616?wireFormat.maxInactivityDuration=0)

+ 40 - 0
cms/cms-mq/src/main/webapp/WEB-INF/web.xml

@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://java.sun.com/xml/ns/javaee"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+         version="2.5">
+
+  <!-- 强制进行转码 -->
+  <filter>
+    <filter-name>CharacterEncodingFilter</filter-name>
+    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
+    <init-param>
+      <param-name>encoding</param-name>
+      <param-value>UTF-8</param-value>
+    </init-param>
+  </filter>
+  <filter-mapping>
+    <filter-name>CharacterEncodingFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+    <dispatcher>REQUEST</dispatcher>
+    <dispatcher>FORWARD</dispatcher>
+  </filter-mapping>
+
+  <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
+  <listener>
+    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+  </listener>
+  <context-param>
+    <param-name>contextConfigLocation</param-name>
+    <param-value>
+      classpath*:applicationContext*.xml
+    </param-value>
+  </context-param>
+
+  <!-- 日志配置文件 -->
+  <context-param>
+    <param-name>log4jConfigLocation</param-name>
+    <param-value>classpath:log4j.properties</param-value>
+  </context-param>
+
+</web-app>

+ 1 - 0
cms/pom.xml

@@ -18,6 +18,7 @@
         <module>cms-dao</module>
         <module>cms-service</module>
         <module>cms-web</module>
+        <module>cms-mq</module>
     </modules>
 
     <dependencyManagement>