(二)多线程下的生产者消费者

上一篇是大体思路,以后都是一些小细节,本节是新增任务所要实现的接口等等

1. 新增任务必须要实现的工厂类

package com.joker.factory;

import com.joker.bean.SQLConnect;
import com.joker.parameter.StatementParameters;

/**
 * 必须要实现的工厂类
 *
 * @author Joker
 * @since 2021/12/05
 */
public interface TaskFactory {

	String getSql();

	Class<? extends SQLConnect> getSqlConnectClass();

	StatementParameters getPrepareStatementParameters();
}

2. 新增任务

package com.joker.factory;

import com.joker.bean.LegalContract;
import com.joker.bean.SQLConnect;
import com.joker.parameter.LegalContractParameters;
import com.joker.parameter.StatementParameters;

/**
 * @author Joker
 * @since 2021/12/05
 */
public class LegalContractFactory implements TaskFactory {

	@Override
	public String getSql() {
		return "insert into tb_legal_contract_info_qqqqqq (            lg_id, contract_type, contract_name, contract_id, contractor_id,            contractor_name, contract_amount, contract_company, win_time,            effective_time, ecp_code, old_contract_id, undertaker_orgname, undertaker_deptname,            contract_state_code, is_caigou, caigou_type, erpcode, ht_sum_adjust, ht_sum_chang, inputuser, inputtime)            values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	}

	@Override
	public Class<? extends SQLConnect> getSqlConnectClass() {
		return LegalContract.class;
	}

	@Override
	public StatementParameters getPrepareStatementParameters() {
		return new LegalContractParameters();
	}
}

3.工厂类必须实现的接口

package com.joker.bean;

/**
 * 实体类必须实现的接口
 *
 * @author Joker
 * @since 2021/12/05
 */
public interface SQLConnect {

	String[] generateParameters();

	boolean check();
}

4.新增任务实体类

package com.joker.bean;

import com.joker.utils.FieldUtil;

import java.io.Serializable;

/**
 * @author Joker
 * @since 2021/12/05
 */
public class LegalContract implements SQLConnect, Serializable, Cloneable {
	String undertaker_org;
	String code;
	String erpcode;
	String all_parties;
	String sum;
	String caigou_type;
	String sum_adjust;
	String undertaker;
	String old_cnt_code;
	String sum_chang;
	String inure_date;
	String cnt_type_id;
	String stamp_tax_code;
	String is_caigou;
	String name;
	String undertaker_id;
	String bid_time;
	String undertaker_dept;
	String state_code;
	String ecp_contract_id;

	'''
	settter
	getter
	'''

	@Override
	public String[] generateParameters() {
		//用来拼接SQL所需参数
		String[] temp = new String[19];
		temp[0] = this.cnt_type_id;
		temp[1] = this.name;
		temp[2] = this.code;
		temp[3] = this.undertaker_id;
		temp[4] = this.undertaker;
		temp[5] = this.sum;
		temp[6] = this.all_parties;
		temp[7] = this.bid_time;
		temp[8] = this.inure_date;
		temp[9] = this.ecp_contract_id;
		temp[10] = this.old_cnt_code;
		temp[11] = this.undertaker_org;
		temp[12] = this.undertaker_dept;
		temp[13] = this.state_code;
		temp[14] = this.is_caigou;
		temp[15] = this.caigou_type;
		temp[16] = this.erpcode;
		temp[17] = this.sum_adjust;
		temp[18] = this.sum_chang;
		return temp;
	}

	@Override
	public boolean check() {
		//实现自身校验规则
		return FieldUtil.hasText(this.code);
	}

	@Override
	public LegalContract clone() {
		try {
			return (LegalContract) super.clone();
		} catch (CloneNotSupportedException var2) {
			throw new AssertionError();
		}
	}
}

5. 工厂类必须实现的接口,用来获取SQL所需参数

package com.joker.parameter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 必须实现的接口,用来获取SQL所需参数
 *
 * @author Joker
 * @since 2021/12/05
 */
public interface StatementParameters {
	void setParameters(PreparedStatement ps, String... parameters) throws SQLException;
}

6. 参数

package com.joker.parameter;

import com.joker.utils.FieldUtil;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Joker
 * @since 2021/12/05
 */
public class LegalContractParameters implements StatementParameters {

	@Override
	public void setParameters(PreparedStatement ps, String... parameters) throws SQLException {
		//为SQL拼接参数
		ps.setString(1, FieldUtil.getRandomId());
		ps.setString(2, FieldUtil.getText(parameters[0]));
		ps.setString(3, FieldUtil.getText(parameters[1]));
		ps.setString(4, FieldUtil.getText(parameters[2]));
		ps.setString(5, FieldUtil.getText(parameters[3]));
		ps.setString(6, FieldUtil.getText(parameters[4]));
		ps.setBigDecimal(7, FieldUtil.getBigDecimal(parameters[5]));
		ps.setString(8, FieldUtil.getText(parameters[6]));
		ps.setDate(9, FieldUtil.WrapSqlDate(parameters[7]));
		ps.setDate(10, FieldUtil.WrapSqlDate(parameters[8]));
		ps.setString(11, FieldUtil.getText(parameters[9]));
		ps.setString(12, FieldUtil.getText(parameters[10]));
		ps.setString(13, FieldUtil.getText(parameters[11]));
		ps.setString(14, FieldUtil.getText(parameters[12]));
		ps.setString(15, FieldUtil.getText(parameters[13]));
		ps.setString(16, FieldUtil.getText(parameters[14]));
		ps.setString(17, FieldUtil.getText(parameters[15]));
		ps.setString(18, FieldUtil.getText(parameters[16]));
		ps.setString(19, FieldUtil.getText(parameters[17]));
		ps.setBigDecimal(20, FieldUtil.getBigDecimal(parameters[18]));
		ps.setString(21, "数据中台");
		ps.setString(22, FieldUtil.now());
	}
}

本节完毕
下一篇:[(三)多线程下的生产者消费者](https://www.cnblogs.com/zqm-sau/p/15650117.html)

上一篇:分享一个Fluent风格的邮件发送封装类


下一篇:iOS 图像渲染原理