Java's new project learning online notes-day6

Keywords: Java JSON RabbitMQ Spring xml

1.3 page publishing manufacturer
1.3.1 demand analysis
The administrator publishes the consumption of "page publishing" through cms system, which is the producer of page publishing.
The requirements are as follows:
1. The administrator enters the management interface and clicks "page publishing". The front end requests the cms page publishing interface. 2. The cms page publishing interface performs page static and stores the static page in GridFS.
3. After the static is successful, send the message published on the page to the message queue. 1) obtain the information of the page and the site ID of the page. 2) set the message content to page ID. (json format is adopted to facilitate future expansion)
3) send a message to the ex ﹣ CMS ﹣ postpage switch, and use the site ID as routingKey. 1.3.2 RabbitMQ configuration
1. Configure the connection parameters of Rabbitmq
Add the following configuration in application.yml:

[mw_shl_code=applescript,true]spring: 
rabbitmq:    
host: 127.0.0.1    
port: 5672   
username: guest     
password: guest     
virtualHost: /
[/mw_shl_code]

2. Add dependency in pom.xml

[mw_shl_code=applescript,true]<dependency>
    <groupId>org.springframework.boot</groupId>  
  <artifactId>spring‐boot‐starter‐amqp</artifactId>
</dependency>
[/mw_shl_code]

3. RabbitMQCon "g configuration
Because cms, as a page publisher, has to face many servers of different sites and many page publishing queues, there is no need to configure queues here, just configure switches.
In cms project, only switch name is configured.

[mw_shl_code=applescript,true]package com.xuecheng.manage_cms.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;
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;   
  @Configuration public class RabbitmqConfig { 
   //Name of the switch 
   public static final String EX_ROUTING_CMS_POSTPAGE="ex_routing_cms_postpage";  
   /** 
    * Switch configuration uses direct type  
    * @return the exchange    
  */   
@Bean(EX_ROUTING_CMS_POSTPAGE) 
   public Exchange EXCHANGE_TOPICS_INFORM() { 
       return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE).durable(true).build(); 
    } }
[/mw_shl_code]

1.3.3 Api interface
Publish the interface on the api project definition page:

[mw_shl_code=applescript,true]@ApiOperation("Publish page")
public ResponseResult post(String pageId);
[/mw_shl_code]

1.3.4 PageService
Define the page publishing method in PageService. The code is as follows:

[mw_shl_code=applescript,true]//Page Publishing
public ResponseResult postPage(String pageId){  
  //Perform staticization 
    String pageHtml = this.getPageHtml(pageId);  
   if(StringUtils.isEmpty(pageHtml)){
        ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);  
   }   
//Save the static file   
  CmsPage cmsPage = saveHtml(pageId, pageHtml);   
  //send message   
  sendPostPage(pageId);  
   return new ResponseResult(CommonCode.SUCCESS);
} //Send page publishing message private void sendPostPage(String pageId){
CmsPage cmsPage = this.getById(pageId);  
   if(cmsPage == null){     
  ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS); 
   } 
   Map<String,String> msgMap = new HashMap<>();  
   msgMap.put("pageId",pageId);
     //Message content String msg = JSON.toJSONString(msgMap);  
   //Get site id as routingKey  
   String siteId = cmsPage.getSiteId();  
   //Publish news  
   this.rabbitTemplate.convertAndSend(RabbitmqConfig.EX_ROUTING_CMS_POSTPAGE,siteId, msg); } //Save static page content private CmsPage saveHtml(String pageId,String content){ 
   //Query page    
Optional<CmsPage> optional = cmsPageRepository.findById(pageId);   
  if(!optional.isPresent()){  
     ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS); 
    }  
CmsPage cmsPage = optional.get();   
  //Delete before storing  
  String htmlFileId = cmsPage.getHtmlFileId();  
   if(StringUtils.isNotEmpty(htmlFileId)){   
     gridFsTemplate.delete(Query.query(Criteria.where("_id").is(htmlFileId)));   
  }   
//Save html file to GridFS  
   InputStream inputStream = IOUtils.toInputStream(content); 
   ObjectId objectId = gridFsTemplate.store(inputStream, cmsPage.getPageName());   
  //File ID string fileid = objectid. Tostring();   
  //Store the file id in cmspage   
  cmsPage.setHtmlFileId(fileId);   
  cmsPageRepository.save(cmsPage);  
   return cmsPage;   }
[/mw_shl_code]

1.3.5 CmsPageController
Write Controller to implement api interface, receive page request, call service to execute page publishing.

[mw_shl_code=applescript,true]@Override
@PostMapping("/postPage/{pageId}") public ResponseResult post(@PathVariable("pageId") String pageId)
{ 
   return pageService.postPage(pageId); }
[/mw_shl_code]

Posted by kjb on Thu, 05 Dec 2019 08:58:47 -0800