Commit fe3aaee6 authored by jianhua.zhang's avatar jianhua.zhang

波场浏览器JAVA版

parent dee5d47e
......@@ -124,6 +124,7 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
......
......@@ -23,6 +23,13 @@ public interface BlockDayCensusRepository {
*/
int insert(@Param("record") BlockDayCensus record);
/**
* 删除记录
* @param id
* @return
*/
void deleteById(@Param("id") Long id);
/**
* 按照时间统计区块信息
*
......
package com.wuban.tron.explore.fetch;
import com.wuban.tron.explore.domain.TronAccount;
import com.wuban.tron.explore.entity.Address;
import com.wuban.tron.explore.service.AddressService;
import com.wuban.tron.explore.service.TronService;
import com.wuban.tron.explore.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* <core>账户余额处理者服务接口方法实现类</core>
*
* @author sky
* @date 2020/11/04
*/
@Slf4j
@Deprecated
public class AccountBalanceTask extends AbstractJob {
private AddressService addressService = SpringContextUtil.getBean(AddressService.class);
private static final int SIZE = 2;
private String name = "账户余额持久化线程池";
private static ThreadPoolExecutor threadPool;
private TronService tronService = SpringContextUtil.getBean(TronService.class);
private Set<String> dataSet;
public AccountBalanceTask() {
initPool();
}
public AccountBalanceTask(Set<String> dataSet) {
this.dataSet = dataSet;
}
public void fetch(String address) {
TronAccount tronAccount = this.tronService.getAccount(address);
if (tronAccount != null) {
if (tronAccount.getBalance() != null) {
Address obj = new Address();
obj.setAddress(address);
obj.setBalance(tronAccount.getBalance());
this.addressService.updateById(obj);
log.info("更新账户余额 account:{}", obj.toString());
}
}
}
@Override
public boolean execute() {
if (!CollectionUtils.isEmpty(dataSet)) {
dataSet.forEach(o -> threadPool.execute(() -> fetch(o)));
}
return true;
}
public synchronized void initPool() {
if (threadPool == null) {
threadPool = new ThreadPoolExecutor(SIZE, SIZE*2, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
new ExecutorThreadFactory());
log.info("初始化线程池 name:{} coreSize:{} maxSize:{}", name, SIZE, SIZE*2);
}
}
/**
* The match engine thread factory
*/
private class ExecutorThreadFactory implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
ExecutorThreadFactory() {
final SecurityManager s = System.getSecurityManager();
this.group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
this.namePrefix = "excutor-" + AccountBalanceTask.this.name + "-thread-";
}
@Override
public Thread newThread(final Runnable r) {
final Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) { t.setDaemon(false); }
if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); }
return t;
}
}
}
......@@ -7,6 +7,7 @@ import com.wuban.tron.explore.constant.PageConstant;
import com.wuban.tron.explore.dao.*;
import com.wuban.tron.explore.domain.*;
import com.wuban.tron.explore.entity.*;
import com.wuban.tron.explore.entity.example.BlockDayCensusExample;
import com.wuban.tron.explore.entity.example.TransactionExample;
import com.wuban.tron.explore.service.TransactionService;
import com.wuban.tron.explore.util.BigDecimalUtil;
......@@ -401,11 +402,22 @@ public class TransactionServiceImpl implements TransactionService {
BigDecimal averBlockBytes = BigDecimalUtil.getDevide(new BigDecimal(data.getTotalBlockBytes()), new BigDecimal(data.getGenBlockTotalNum()));
data.setAverBlockBytes(averBlockBytes.intValue());
this.blockDayCensusRepository.insert(data);
this.saveCensusData(data);
log.info("date:{} 交易数据已统计完成",date);
} else {
log.info("date:{} 暂无交易数据统计",date);
}
}
private void saveCensusData(BlockDayCensus data) {
BlockDayCensusExample example = new BlockDayCensusExample();
example.createCriteria().andCensusDateEqualTo(data.getCensusDate());
List<BlockDayCensus> list = this.blockDayCensusRepository.selectByExample(example);
if (!CollectionUtils.isEmpty(list)) {
Long id = list.get(0).getId();
this.blockDayCensusRepository.deleteById(id);
}
this.blockDayCensusRepository.insert(data);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment