.商品审核-执行网页静态化 6.1 需求分析 运用消息中间件 activeMQ 实现运营商后台与网页生成服务的零耦合。运营商执行商品审核后,向 activeMQ 发送消息(商品 ID),网页生成服务从 activeMQ 接收到消息后执行网页生成操作。 6.1 消息生产者(运营商后台) 6.1.1 解除耦合 修改 pinyougou-manager-web,移除网页生成服务接口依赖:


<dependency>
 
<groupId>com.pinyougou</groupId>
 
<artifactId>pinyougou-page-interface</artifactId>
 
<version>0.0.3-SNAPSHOT</version>
 
</dependency>
GoodsController.java 中删除调用网页生成服务接口的相关代码

//private  ItemPageService  itemPageService;
//静态页生成
 
//for(Long  goodsId:ids){
 
//itemPageService.genItemHtml(goodsId);
 
//}

6.1.1 准备工作
修改配置文件 spring-activemq.xml,添加配置

<!--这个是订阅模式 文本信息-->
 
<bean  id="topicPageDestination" class="org.apache.activemq.command.ActiveMQTopic">
 
<constructor-arg  value="pinyougou_topic_page"/>
 
</bean>
6.1.1 代码实现
修改 pinyougou-manager-web 的 GoodsController.java

public  Result  updateStatus(Long[]  ids,String  status){
 
 
try {
 
 
.......
 
if(status.equals("1")){//审核通过
 
........
 
//静态页生成
for(final  Long  goodsId:ids){ jmsTemplate.send(topicPageDestination,  new 
@Override
 
 
public  Message  createMessage(Session  session)  throws
JMSException  {
 
 
return  session.createTextMessage(goodsId+"");
 
 
}
 
 
});
 
 
}
 
 
}
 
 
......
 
 
}  catch  (Exception  e)  {
 
 
......
 
 
}
 
 
}
6.3 消息消费者(页面生成服务)
6.3.1 解除 dubbox依赖
(1)修改工程 pinyougou-page-service ,删除 dubbox 相关依赖

<!-- dubbo 相 关 -->
 
<dependency>
 
<groupId>com.alibaba</groupId>
 
<artifactId>dubbo</artifactId>
 
</dependency>
<dependency>
 
<groupId>org.apache.zookeeper</groupId>
 
<artifactId>zookeeper</artifactId>
 
</dependency>
 
<dependency>
 
<groupId>com.github.sgroschupf</groupId>
 
<artifactId>zkclient</artifactId>
 
</dependency>
(2)修改 applicationContext-service.xml,删除 dubbox 相关配置

<dubbo:protocol  name="dubbo"  port="20885"></dubbo:protocol>
 
<dubbo:application  name="pinyougou-page-service"/>
 
<dubbo:registry  address="zookeeper://192.168.25.135:2181"/>
 
<dubbo:annotation  package="com.pinyougou.page.service.impl"  />
(3)修改 ItemPageServiceImpl 类的@Service 注解 为 org.springframework.stereotype.Service
包下的@Service 注解
6.3.1 准备工作
(1)修改 applicationContext-service.xml,添加配置

<context:component-scan  base-package="com.pinyougou.page.service.impl"/>
(2)pom.xml 中引入 activeMQ 客户端的依赖

<dependency>
 
<groupId>org.apache.activemq</groupId>
 
<artifactId>activemq-client</artifactId>
 
<version>5.13.4</version>
</dependency>
(3)添加 spring 配置文件 applicationContext-jms-consumer.xml

<!--  真正可以产生 Connection 的 ConnectionFactory,由对应的  JMS 服务厂商提供-->
 
<bean  id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 
<property  name="brokerURL"  value="tcp://192.168.25.135:61616"/>
 
 
</bean>
 
<!--  Spring 用于管理真正的 ConnectionFactory 的 ConnectionFactory  -->
 
<bean  id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
 
<property  name="targetConnectionFactory"  ref="targetConnectionFactory"/>
 
 
</bean>
 
<!--这个是队列目的地,点对点的 -->
 
<bean  id="topicPageDestination" class="org.apache.activemq.command.ActiveMQTopic">
 
<constructor-arg  value="pinyougou_queue_page"/>
 
 
</bean>
 
<!-- 消息监听容器 -->
 
<bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
 
 
<property  name="connectionFactory"  ref="connectionFactory"  />
 
 
<property  name="destination"  ref="topicPageDestination"  />
 
 
<property  name="messageListener"  ref="pageListener"  />
 
 
</bean>
6.3.1 代码编写
创建消息监听类 PageListener

@Component
 
 
public  class  PageListener  implements  MessageListener  {
 
 
 
 
@Autowired
 
 
private  ItemPageService  itemPageService;
 
 
 
 
@Override
 
 
public  void  onMessage(Message  message)  { TextMessage  textMessage=  (TextMessage)message; try {
String  text  =  textMessage.getText();
 
System.out.println("接收到消息:"+text);
 
boolean  b  =  itemPageService.genItemHtml(Long.parseLong(text));
 
 
}  catch  (Exception  e)  { e.printStackTrace();
}
 
 
}
 
 
}