Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package x7.repository.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import x7.core.async.HeartBeator;
import x7.core.async.HeartBeat;
import x7.core.bean.BeanElement;
import x7.core.bean.Parsed;
import x7.core.bean.Parser;
import x7.core.repository.X;
import x7.core.util.TimeUtil;
import x7.repository.mapper.Mapper;
import x7.repository.mapper.MapperFactory;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
*
* @author Sim
*
*/
@Component
public class AsyncDaoImpl implements HeartBeat, AsyncDao {
/**
* 批处理最多条数
*/
public static int MAX_BATCH = 500;
/**
* 延时1分钟
*/
private final static int HEARTBEAT_DELAY = (int) TimeUtil.ONE_MINUTE;
private long heartBeatTime = 0; // 降低心跳的实时性来提高性能
private final ExecutorService mainExecutor = Executors.newSingleThreadExecutor();
private final ExecutorService inner = Executors.newSingleThreadExecutor();
public AsyncDaoImpl() {
HeartBeator.add(this);
}
@Autowired
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@SuppressWarnings("rawtypes")
private Map> creationMap = new HashMap>();
@SuppressWarnings("rawtypes")
private Map> refreshMap = new HashMap>();
@SuppressWarnings("rawtypes")
private Map> removeMap = new HashMap>();
private Connection getConnection() throws SQLException {
if (dataSource == null){
System.err.println("No DataSource");
}
return dataSource.getConnection();
}
private static void close (PreparedStatement pstmt){
if (pstmt != null){
try{
pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 放回连接池,
* 连接池已经重写了关闭连接的方法
*/
private static void close(Connection conn) {
try {
if (conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
*
*/
@Override
public void create(final Object obj) {
mainExecutor.submit(new Runnable() {
@SuppressWarnings("rawtypes")
@Override
public void run() {
/*
* 加入需要持久化的的对象MAP
*/
Class clz = obj.getClass();
filterTryToCreate(clz);
ArrayList