A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】学成在线 第6天 讲义-页面发布 课程管理二

1.2.3 RabbitmqConfifig配置类
消息队列设置如下:
1、创建ex_cms_postpage交换机
2、每个Cms Client创建个队列与交换机绑定
3、每个Cms Client程序配置队列名称和routingKey,将站点ID作为routingKey
[AppleScript] 纯文本查看 复制代码
package com.xuecheng
.manage_cms_client.config;
import org
.springframework.amqp
.core.*
;
import org
.springframework.beans.factory
.annotation.
Qualifier;
import org
.springframework.beans.factory
.annotation.Value;
import org
.springframework.context.annotation.Bean;
import org
.springframework.context.annotation.Configuration;
/
**
*
@author Administrator
*
@version 1.0
**
/
@Configuration
public class RabbitmqConfig {
//队列bean的名称
public static final String QUEUE_CMS_POSTPAGE =
"
queue_cms_postpage
"
;
//交换机的名称
public static final String EX_ROUTING_CMS_POSTPAGE=
"
ex_routing_cms_postpage
"
;
//队列的名称
@Value(
"
${xuecheng
.mq
.
queue}
"
)
public String queue_cms_postpage_name;
//routingKey 即站点Id
@Value(
"
${xuecheng
.mq
.routingKey}
"
)
public String routingKey;
/
**
*
交换机配置使用direct类型
*
@return the exchange
*
/
@Bean(EX_ROUTING_CMS_POSTPAGE)
public Exchange EXCHANGE_TOPICS_INFORM() {
return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE)
.durable(true)
.build();
}
//声明队列
@Bean(QUEUE_CMS_POSTPAGE)
public Queue QUEUE_CMS_POSTPAGE() {
Queue queue
= new Queue(queue_cms_postpage_name);
return queue;
}
/
**
*
绑定队列到交换机
*
*
@param queue the queue
*
@param exchange the exchange
*
@return the binding
*
/
@Bean
public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_CMS_POSTPAGE) Queue queue,
@Qualifier(EX_ROUTING_CMS_POSTPAGE) Exchange exchange) {
return BindingBuilder.bind(queue)
.to(exchange)
.with(routingKey)
.noargs();
}
}

1.2.4 定义消息格式
消息内容采用json格式存储数据,如下:
页面id:发布页面的id
[AppleScript] 纯文本查看 复制代码
 {
"
pageId
"
:
""
}


1.2.5 PageDao
1、使用CmsPageRepository 查询页面信息

[AppleScript] 纯文本查看 复制代码
public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
}

2、使用CmsSiteRepository查询站点信息,主要获取站点物理路径
[AppleScript] 纯文本查看 复制代码
 public interface CmsSiteRepository extends MongoRepository<CmsSite,String> {
}


1.2.6 PageService
Service中定义保存页面静态文件到服务器物理路径方法:
[AppleScript] 纯文本查看 复制代码
 package com.xuecheng
.manage_cms_client.service;
import com.mongodb.client.
gridfs.GridFSBucket;
import com.mongodb.client.
gridfs.GridFSDownloadStream;
import com.mongodb.client.
gridfs.model.GridFSFile;
import com.xuecheng
.framework.domain.cms.CmsPage;
import com.xuecheng
.framework.domain.cms.CmsSite;
import com.xuecheng
.framework.domain.cms.response.CmsCode;
import com.xuecheng
.framework.exception.ExceptionCast;
import com.xuecheng
.manage_cms_client.dao.CmsPageRepository;
import com.xuecheng
.manage_cms_client.dao.CmsSiteRepository;
import org
.apache.commons.io.IOUtils;
import org
.springframework.beans.factory
.annotation.Autowired;
import org
.springframework.data.mongodb.core.
query
.Criteria;
import org
.springframework.data.mongodb.core.
query
.
Query;
import org
.springframework.data.mongodb.
gridfs.GridFsResource;
import org
.springframework.data.mongodb.
gridfs.GridFsTemplate;
import org
.springframework.stereotype.Service;
import java.io.*
;
import java.util.Optional;
/
**
*
@author Administrator
*
@version 1.0
**
/
@Service
public class PageService {
@Autowired
CmsPageRepository cmsPageRepository;
@Autowired
CmsSiteRepository cmsSiteRepository;
@Autowired
GridFsTemplate gridFsTemplate;
@Autowired
GridFSBucket gridFSBucket;
//将页面html保存到页面物理路径
public void savePageToServerPath(String pageId){
Optional<CmsPage> optional =
cmsPageRepository
.findById(pageId);
if(!optional.isPresent()){
ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
}
//取出页面物理路径
CmsPage cmsPage
=
optional.
get();
//页面所属站点
CmsSite cmsSite
=
this.
getCmsSiteById(cmsPage.
getSiteId());
//页面物理路径
String pagePath =
cmsSite.
getSitePhysicalPath() + cmsPage.
getPagePhysicalPath() +
cmsPage.
getPageName();
//查询页面静态文件
String htmlFileId
=
cmsPage.
getHtmlFileId();
InputStream inputStream =
this.
getFileById(htmlFileId);
if(inputStream == null){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
}
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(new File(pagePath));
//将文件内容保存到服务物理路径
IOUtils.copy(inputStream,fileOutputStream);
} catch (Exception e) {
e.
printStackTrace();
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.
printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.
printStackTrace();
}
}
}
//根据文件id获取文件内容
public InputStream getFileById(String fileId){
try {
GridFSFile gridFSFile
=
gridFsTemplate.findOne(Query
.
query(Criteria.where(
"
_id
"
)
.is(fileId)));
GridFSDownloadStream gridFSDownloadStream =
gridFSBucket.openDownloadStream(gridFSFile.
getObjectId());
GridFsResource gridFsResource
= new GridFsResource(gridFSFile,gridFSDownloadStream);
return gridFsResource.
getInputStream();
} catch (IOException e) {
e.
printStackTrace();
}
return null;
}
//根据站点id得到站点
public CmsSite getCmsSiteById(String siteId){
Optional<CmsSite> optional =
cmsSiteRepository
.findById(siteId);
if(optional.isPresent()){
CmsSite cmsSite
=
optional.
get();
return cmsSite;
}
return null;
}
}


1.2.6ConsumerPostPage
cms client工程的mq包下创建ConsumerPostPage类,ConsumerPostPage作为发布页面的消费客户端,监听页面发布队列的消息,收到消息后从mongodb下载文件,保存在本地。
[AppleScript] 纯文本查看 复制代码
 package com.xuecheng
.manage_cms_client.mq;
import com.alibaba.fastjson.JSON;
import com.xuecheng
.framework.domain.cms.CmsPage;
import com.xuecheng
.manage_cms_client.dao.CmsPageRepository;
import com.xuecheng
.manage_cms_client.service.PageService;
import org
.slf4j
.Logger;
import org
.slf4j
.LoggerFactory;
import org
.springframework.amqp
.rabbit.annotation.RabbitListener;
import org
.springframework.beans.factory
.annotation.Autowired;
import org
.springframework.stereotype.Component;
import java.util.Map;
import java.util.Optional;
/
**
*
@author Administrator
*
@version 1.0
**
/
@Component
public class ConsumerPostPage {
private static final Logger LOGGER = LoggerFactory
.
getLogger(ConsumerPostPage.class);
@Autowired
CmsPageRepository cmsPageRepository;
@Autowired
PageService pageService;
@RabbitListener(queues
=
{
"
${xuecheng
.mq
.
queue}
"
})
public void postPage(String msg){
//解析消息
Map map
=
JSON.
parseObject(msg, Map
.class);
LOGGER.info(
"
receive cms post page:{}
"
,msg
.toString());
//取出页面id
String pageId
=
(String) map
.
get(
"
pageId
"
);
//查询页面信息
Optional<CmsPage> optional =
cmsPageRepository
.findById(pageId);
if(!optional.isPresent()){
LOGGER.error(
"
receive cms post page,cmsPage is null:{}
"
,msg
.toString());
return ;
}
//将页面保存到服务器物理路径
pageService.savePageToServerPath(pageId);
}
}



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马