Back end development tips

Back end development tips (7)

1. Automatically update tasks

1, Start the program and add comments

@EnableScheduling
@SpringBootApplication
@MapperScan("cn.piesat.sar.dao")
public class MessageApplication {
    public static void main(String[] args) {

        SpringApplication.run(MessageApplication.class, args);
    }

}

2, quartz/ AutoScanTask injects Mapper of other services

  
@Component
public class AutoScanTask {

    private final Logger logger = LoggerFactory.getLogger(AutoScanTask.class);


    @Autowired
    private ThematicTaskMapper taskInfoMapper;


    @Autowired
    private RedisUtil redisUtil;
     /** 
     * It is called every 10 minutes. The program starts with a delay of 10s and executes fixedRate for 1000 milliseconds. It is called every time
     * initalDelay How long is the delay
     */
    @Scheduled(fixedRate = 10 * 60 * 1000, initialDelay = 10 * 1000)
    public void updateTaskStatus() {
        System.out.println("Auto start");
        try {
            //Query the tasks to be executed or under execution in the specified time period in the database
            Integer unfinishedTaskCount = taskInfoMapper.getUnfinishedTaskCount();
            logger.info(String.format("Automatic task scanning for qualified[%d]Main task", unfinishedTaskCount));
            if (unfinishedTaskCount != null && unfinishedTaskCount > 0) {

                List<ThematicTask> taskInfoList = taskInfoMapper.getUnfinishedTask();
                if (taskInfoList != null && taskInfoList.size() > 0) {
                        for (ThematicTask taskInfo : taskInfoList) {
                            taskInfo.setStatus(1);
                            taskInfoMapper.updateSubTaskStatus(taskInfo);
                            System.out.println("Update status succeeded"+taskInfo.toString());
//                          Update task status
//                          updateSubTaskStatus(taskInfo);
                        }
                    }
//                }
            }
        } catch (Exception e) {
            logger.error("Update task workflow status error:", e);
        }
    }
    
}

3, Call mapper under other services xml

<!--
//    Number of unfinished tasks
    Integer getUnfinishedTaskCount();

//    Unfinished tasks
    List<ThematicTask> getUnfinishedTask();
//    Update task status
    int updateSubTaskStatus(ThematicTask taskInfo);
-->
<!--    &lt;!&ndash; Query the number of unfinished main tasks &ndash;&gt;-->
    <select id="getUnfinishedTaskCount" resultType="Integer">
        SELECT COUNT(1)
        FROM sar_thematic_product_task
        WHERE "status" != 3 AND "status" != 4;
    </select>
    <select id="getUnfinishedTask" parameterType="java.util.HashMap" resultType="cn.piesat.sar.entity.ThematicTask">
        SELECT
        s1.id, s1.require_task_id "requireTaskId", s1.priority, s1.special_product_level "specialProductLevel", s1.standard_product_list "standardProductList", s1.receive_time "receiveTime", s1.status, s1.rtask_result "rtaskResult", s1.rtask_progress "rtaskProgress",s1.msg_type "msgType"
        FROM sar_thematic_product_task s1
        where
        s1.status=0
        order by s1.receive_time desc
        <if test="pageNum != null and pageSize != null and pageSize > 0">
            limit #{pageSize} OFFSET #{pageNum}
        </if>
    </select>

    <update id="updateSubTaskStatus">
        UPDATE sar_thematic_product_task
        <set>
            <if test="status != null and status != ''">
                status=#{status}
            </if>
        </set>
        WHERE id = #{id}
    </update>

2. Message service

1, Store message reception ServiceImpl

package cn.piesat.sar.service.impl;

import cn.piesat.sar.base.Config;
import cn.piesat.sar.entity.ServiceResult;
import cn.piesat.sar.service.TaskMessageService;
import cn.piesat.sar.utils.ClientConfiguration;
import cn.piesat.sar.utils.TestData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class TaskMessageServicempl implements TaskMessageService {

    @Autowired
    TestData testData; // Tool test data
    @Autowired
    Config config; // route
    @Autowired
    ClientConfiguration clientConfiguration; //Request tool class
    @Value("${task.alg.param}")
    private String taskAlgParam; //File verification rules

    public void sendDataNotifyMessage(String msg) {

        Map <String,Object> param = new HashMap<>();
        param.put("appKey","");
        param.put("bizCode","");
        param.put("message",msg);
        //Send data completion notification message
        String mqUrl = config.getMqApi()+"send";
        clientConfiguration.sendPost(mqUrl,JSON.toJSONString(param, SerializerFeature.WriteMapNullValue));
    }

    @Override
    public ServiceResult receiveThematicProductTaskMessage(String msg) {
        ServiceResult result = new ServiceResult(false);
        try {
            String taskMessage = testData.getTaskMessage();//Simulation task tracking message

            String url = config.getDataServiceUrl()+"thematictask/add";
            JSONObject jsonObject = clientConfiguration.sendPost(url,taskMessage);
            if(jsonObject.getBoolean("success")){
                result.setMessage("Message processing succeeded");
                result.setSuccess(true);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("Message processing failed");
        }
        return result;
    }

    private void taskMessageToStorage(String taskMessage, boolean flag) {
        String thematicTaskUrl = config.getDataServiceUrl()+"thematictask/add";
        JSONObject jsonObject = JSONObject.parseObject(taskMessage);
        JSONObject param = new JSONObject();
        param.put("requireTaskId",jsonObject.getString("requireTaskId"));
        param.put("priority",jsonObject.getString("priority"));
        param.put("targetLevel",jsonObject.getString("targetLevel"));
        param.put("geoJsonString",jsonObject.getString("geoJsonString"));

        param.put("standardProductList",jsonObject.getString("standardProductList"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        param.put("receiveTime",jsonObject.getString(  sdf.format(new Date())));

        if(flag){
            param.put("status",0);
        }else {
            param.put("status",1);
        }
        clientConfiguration.sendPost(thematicTaskUrl,JSON.toJSONString(param));
    }


}

3. pg sql PK auto increment

1, Set primary key auto increment when creating a table

mysql primary key auto increment_ Use the keyword "insert" and "creql".

[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-7y3zzzx1-1649897596118) (C: \ users \ htht \ appdata \ roaming \ typora \ typora user images \ image-202204011131257039. PNG)]

2, Modify the id field of the menu table to auto increment the primary key

1. In Postgre sql, ID auto increment is implemented to create an association sequence. Some sql statements create a sequence:

CREATE SEQUENCE menu_id_seq START 6000001;

The sequence name is menu_id_seq, starting with 6000001

2. Then set nextval('menu_id_seq ':: regclass) in the field default value

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-pifsvgzm-1649897596119) (C: \ users \ htht \ appdata \ roaming \ typora user images \ image-202204011313539241. PNG)]

3. Save field attribute changes

ALTER TABLE public.menu ALTER COLUMN id SET DEFAULT nextval('menu_id_seq'::regclass);

3, Self increment starting number of modified id

Take the current maximum id as the starting number of current id self increment

select setval('gx_history_id_seq',(select max(id) from gx_history)) 

supplement

UndefinedTable: relation "* * * * _id_seq" does not exist solution

Looking up some materials, some said that the problem 10 of postgresql9 version had been fixed

Related skills

    Query all auto increment sequences
    SELECT "c"."relname" FROM "pg_class" "c" WHERE "c"."relkind" = 'S';
    pgSQL When importing or creating a table, check whether there is a self incrementing field sequence, and then create it
    CREATE SEQUENCE IF NOT EXISTS tablename_id_seq;
    Create auto increment sequence
    CREATE SEQUENCE tablename_id_seq
    CREATE SEQUENCE tablename_id_seq START 10;
    Delete a self increasing sequence
    DROP SEQUENCE tablename_id_seq
    Update a self increasing sequence
    alter sequence tablename_id_seq restart with 100
    Query next sequence
    select nextval('tablename_id_seq ');
    Use self increasing sequence
    nextval('tablename_id_seq'::regclass)
--------
Copyright notice: This article is CSDN Blogger「Palm guard shell」Original articles, follow CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement for reprint.
Original link: https://blog.csdn.net/qq_35771567/article/details/103616842

4. Message receiving and verifying message integrity ALG

1, ServiceImpl

 /**
     * Verification rules
     * File example: jh0451_ HT1A_ 0001_ 1024_ 1_ 20220211150123_ twenty trillion and two hundred and twenty billion two hundred and eleven million one hundred and fifty thousand two hundred and twenty-three dat
     1,Whether the number of packages is consistent with the [number of packages] in the interface message;
     2,Whether the data type in each package name is standardized: if the number of packages is 1, the data type should be 0; If the number of packages is 2, the data types in each package cannot be the same. They should be 1 and 2 respectively.
     3,Check separately according to the file package:
     (1)File naming verification
        Whether the station code in the name of each package is consistent with the [station identification] in the interface message;
        Whether the satellite code in the name of each package is consistent with the [satellite code] in the interface message;
        Whether the name of each package is consistent with the [original file name] in the interface message; (solve the requirements by verifying whether the document exists)
     (2)File size verification
     Whether the size of each file package is consistent with the [original file length] in the interface message;
     *
     * */

    public boolean verifyRule(String dataMessage) {
        boolean flag = true;
        JSONObject jsonObject = JSONObject.parseObject(dataMessage);
        JSONArray jsonArray = jsonObject.getJSONArray("fileList");
        int fileNumber = jsonObject.getInteger("fileNumber");
        //Document quantity verification
        if(fileNumber!=jsonArray.size()){
            return false;
        }
        // Package quantity type verification
        if(fileNumber==1){
            String fileName = jsonArray.getJSONObject(0).getString("fileName");
            int dataType = Integer.parseInt(fileName.split("_")[4]);
            if(dataType!=0){
                return false;
            }
        }else if(fileNumber==2){
            for(int i=0;i<jsonArray.size();i++){
                String fileName = jsonArray.getJSONObject(i).getString("fileName");
                int dataType = Integer.parseInt(fileName.split("_")[4]);
                if(dataType!=2 && dataType!=1){
                    return false;
                }
            }
        }
        //File naming verification
        for(int i=0;i<jsonArray.size();i++){
            JSONObject data = jsonArray.getJSONObject(i);
            String path = config.getDataDir();
            String fileName = data.getString("fileName");
            Integer fileLength = data.getInteger("fileLength");

            //File naming verification
            String antenna = data.getString("antenna");
            String satellite = data.getString("satellite");
            if(!antenna.equals(fileName.split("_")[0])){
                return false;
            }
            if(!satellite.equals(fileName.split("_")[1])){
                return false;
            }
            //File size verification
            File f= new File(path+fileName);
            if (f.exists() && f.isFile()){
                if(f.length()!=fileLength){
                    return false;
                }
            } else {
                return false;
            }
        }
        return flag;
    }


5. Kill the process and restart the task

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8082 was already in use.

Action:

Identify and stop the process that's listening on port 8082 or configure this application to listen on another port.

Use command

netstat -ano |findstr 8082

find

Kill the process

taskkill -PID 19772 -F

6. Common commands for Linux deployment

Start Is the jar test successful

java -jar xxx.jar

Identify whether the service is successful

ksar get svc

[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qyijy49n-1649897596119) (C: \ users \ htht \ appdata \ roaming \ typora user images \ image-20220402151921233. PNG)]

ls directory cd enter file cat view vi edit: wq exit save i insert exit exit exit

If you encounter problems, read the error log first

Common K8S commands

kubectl get svc # View service
kubectl get po # View pod
kubectl logs xxxx # View the log of a pod
kubectl apply -f xxx.yaml # Deploy a service whose configuration file has been written
kubectl delete -f xxx.yaml # Delete a service whose profile has been written
kubectl describe pod xxx # This method can be used to view the error details when the container starts to report an error
kubectl exec -ti xxx bash # Enter a container
kubectl get pv # View persistent volumes
kubectl get node # View cluster status
kubectl get po -o wide # You can view which nodes these containers run on

7. Empty gateway

Read the error log first

Common K8S commands

kubectl get svc # View service
kubectl get po # View pod
kubectl logs xxxx # View the log of a pod
kubectl apply -f xxx.yaml # Deploy a service whose configuration file has been written
kubectl delete -f xxx.yaml # Delete a service whose profile has been written
kubectl describe pod xxx # This method can be used to view the error details when the container starts to report an error
kubectl exec -ti xxx bash # Enter a container
kubectl get pv # View persistent volumes
kubectl get node # View cluster status
kubectl get po -o wide # You can view which nodes these containers run on

7. empty gateway

Tags: Java

Posted by mikew2 on Thu, 14 Apr 2022 13:38:14 +0930