Ola a todos,
Tenho uma classe Main onde passo uma lista de equipamentos para a verificacao dos dados e posteriormente a persistenceida destes dados, em uma lista de 1700 equipamentos eu instancio 150 threads ao mesmo tempo, isso faz com que eu tenha 150 conexões coma base de dados, acho que isso não é interessante. Na minha thread tenho tres metodos em que cada um possiu um dao para a persistencia dos dados.
classe main:package milegateinventory;
import br.com.dao.Dao;
import br.com.gvt.milegateinventory.core.ThreadInventoryMilegate;
import br.com.hibernate.HibernateCore;
import br.com.model.Card;
import br.com.model.Port;
import br.com.model.ProfileMilegate;
import br.com.model.Shelf;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author paulo
*/
public class Main {
private synchronized void pause() {
try {
this.wait(200);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Main() throws FileNotFoundException, IOException {
//List<Shelf> shelves = new DaoShelf(HibernateCore.getSession()).listByModel("Milegate2500");
FileInputStream fileInputStream = new FileInputStream(new File("/home/milegate2500/ip"));
DataInputStream in = new DataInputStream(fileInputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ExecutorService threadPool = Executors.newFixedThreadPool(20);
String strline;
while ((strline = br.readLine()) != null) {
//threadPool.submit(new ThreadInventoryMilegate(new Shelf(strline)));
threadPool.submit(new ThreadInventoryMilegate(new Shelf(strline), daoPort, daoCard, daoProfile));
pause();
}
/**
ExecutorService threadPool = Executors.newFixedThreadPool(200);
for (Shelf s : shelves) {
threadPool.submit(new ThreadInventoryMilegate(s));
}*/
try {
threadPool.shutdown(); //Torna impossível a entrada de novas threads na Pool
if (!threadPool.awaitTermination(20, TimeUnit.MINUTES)) //Aguarda 20 minutos
{
threadPool.shutdownNow();
}
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
new Main();
}
}
[code]
Classe Thread
[code]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.gvt.milegateinventory.core;
import br.com.dao.Dao;
import br.com.exception.DaoException;
import br.com.gvt.milegate.core.Core;
import br.com.gvt.milegate.exception.MilegateConnectionException;
import br.com.gvt.milegate.exception.MilegateException;
import br.com.gvt.milegate.function.PortAdslFunc;
import br.com.gvt.milegate.function.ProfileAdslFunction;
import br.com.gvt.milegate.function.ProfileChanFunction;
import br.com.gvt.milegate.function.ShelfFunc;
import br.com.gvt.milegate.function.UnitFunc;
import br.com.gvt.milegate.telnet.TelnetCore;
import br.com.hibernate.HibernateCore;
import br.com.model.Card;
import br.com.model.Port;
import br.com.model.PortPK;
import br.com.model.ProfileMilegate;
import br.com.model.Shelf;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author paulo
*/
public class ThreadInventoryMilegate extends Thread {
private Shelf shelf;
private TelnetCore telnetCore = new TelnetCore();
private Core core = new Core(telnetCore);
private ShelfFunc shelfFunc = new ShelfFunc(telnetCore);
private UnitFunc unitFunc = new UnitFunc(telnetCore);
private List<Card> cards = null;
private PortAdslFunc portAdslFunc = new PortAdslFunc(telnetCore);
public ThreadInventoryMilegate(Shelf shelf) {
this.shelf = shelf;
}
public void persistenceAdslPorts() {
Dao<Port> dao = new Dao<Port>(HibernateCore.getSession(), Port.class);
for (Card c : cards) {
if (c.getName().matches("SUAD1.*")) {
for (int x = 1; x <= 32; x++) {
try {
HibernateCore.beginTransaction();
Port port = new Port();
PortPK portPK = new PortPK(c.getSlot(), x);
port.setId(portPK);
port.getId().setShelf(shelf);
if (!portAdslFunc.getOperationalStatus(port)) {
port = portAdslFunc.getPortConfiguration(port);
} else {
port = portAdslFunc.getPortConfigurationAndStatus(port);
}
try {
//port.setShelf(shelf);
dao.saveOrUpdate(port);
HibernateCore.commitTransaction();
HibernateCore.getSession().flush();
} catch (DaoException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao recuperar as informacoes da porta: /unit-" + c.getSlot() + "/port-" + x + "(" + shelf.getIp() + ")");
}
}
} else if (c.getName().matches("SUAD3.*")) {
for (int x = 1; x <= 48; x++) {
try {
HibernateCore.beginTransaction();
Port port = new Port();
PortPK portPK = new PortPK(c.getSlot(), x);
port.setId(portPK);
port.getId().setShelf(shelf);
if (!portAdslFunc.getOperationalStatus(port)) {
port = portAdslFunc.getPortConfiguration(port);
} else {
port = portAdslFunc.getPortConfigurationAndStatus(port);
}
try {
//port.setShelf(shelf);
dao.saveOrUpdate(port);
HibernateCore.commitTransaction();
HibernateCore.getSession().flush();
} catch (DaoException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao recuperar as informacoes da porta: /unit-" + c.getSlot() + "/port-" + x + "(" + shelf.getIp() + ")");
}
}
}
}
}
private void persistenceCards() {
Dao<Card> dao = new Dao<Card>(HibernateCore.getSession(), Card.class);
for (Card c : cards) {
HibernateCore.beginTransaction();
try {
c = shelfFunc.getCardFullInformation(c);
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao recuperar informacao do slot : " + c.getSlot() + "(" + shelf.getIp() + ")");
}
try {
c.setShelf(shelf);
dao.saveOrUpdate(c);
HibernateCore.getSession().flush();
} catch (DaoException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
HibernateCore.commitTransaction();
}
}
public boolean isHaveSuad(List<Card> cards) {
for (Card c : cards) {
if (c.getName().matches("SUAD.*")) {
return true;
}
}
return false;
}
public void sendConfigurationBackup() {
try {
shelfFunc.upload("/cfgm/configuration", "10.227.1.1_2.tar");
} catch (MilegateException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void persistenceProfiles() {
ProfileAdslFunction profileAdslFunction = new ProfileAdslFunction();
ProfileChanFunction profileChanFunction = new ProfileChanFunction();
Dao<ProfileMilegate> dao = new Dao<ProfileMilegate>(HibernateCore.getSession(), ProfileMilegate.class);
List<ProfileMilegate> profiles = null;
try {
profiles = shelfFunc.getProfiles();
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao recuperar a listagem dos profiles do shelf: " + shelf.getIp());
}
for (ProfileMilegate p : profiles) {
File file = new File("/home/paulo/" + shelf.getIp() + "/");
file.mkdirs();
if (p.getTypeProfile().equals("Adsl2PortProfile")) {
p.setFileName("/home/paulo/" + shelf.getIp() + "/" + p.getName());
try {
shelfFunc.upload("/cfgm/Profile", p.getFileName());
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao enviar o profile: " + p.getName() + "(" + shelf.getIp() + ")");
}
p.setShelf(shelf);
p = profileAdslFunction.validateAdslProfile(p);
HibernateCore.beginTransaction();
HibernateCore.getSession().flush();
try {
dao.save(p);
} catch (DaoException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
HibernateCore.commitTransaction();
HibernateCore.getSession().flush();
} else if (p.getTypeProfile().equals("Adsl2ChanProfile")) {
p.setFileName("/home/paulo/" + shelf.getIp() + "/" + p.getName());
try {
shelfFunc.upload("/cfgm/Profile", p.getFileName());
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao enviar o profile: " + p.getName() + "(" + shelf.getIp() + ")");
}
p.setShelf(shelf);
p = profileChanFunction.validateChanProfile(p);
HibernateCore.beginTransaction();
try {
dao.save(p);
} catch (DaoException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
HibernateCore.commitTransaction();
HibernateCore.getSession().flush();
}
}
}
public void configureFtpServer() {
try {
shelfFunc.setFtpServer("10.200.17.25", "paulo", "teste");
} catch (MilegateException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run() {
try {
core.connect(shelf.getIp(), 23);
core.configureModeDisplayXml();
try {
cards = shelfFunc.getCardsBrief();
} catch (MilegateException ex) {
LogGeneral.logger.severe("Erro ao carregar a listagem de placas do elemento: " + shelf.getIp());
}
this.persistenceCards();
if (isHaveSuad(cards)) {
this.configureFtpServer();
this.persistenceProfiles();
this.persistenceAdslPorts();
}
core.disconnect();
HibernateCore.closeSession();
} catch (MilegateConnectionException ex) {
Logger.getLogger(ThreadInventoryMilegate.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Dao<Port> daoPort = new Dao<Port>(HibernateCore.getSession(), Port.class);
Dao<Card> daoCard = new Dao<Card>(HibernateCore.getSession(), Card.class);
Dao<ProfileMilegate> daoProfile = new Dao<ProfileMilegate>(HibernateCore.getSession(), ProfileMilegate.class);
E na classe Thread criei um construtor que recebe estes daos, entao em cada metodo eu tentei usar o dao que veio da classe main
private Dao<Port> daoPort;;
private Dao<Card> daoCard;
private Dao<ProfileMilegate> daoProfile;
public ThreadInventoryMilegate(Shelf shelf, Dao<Port> daoPort, Dao<Card> daoCard, Dao<ProfileMilegate> daoProfile) {
this.shelf = shelf;
this.daoPort = daoPort;
this.daoCard = daoCard;
this.daoProfile = daoProfile;
}
Quando eu fiz isso aconteceu algo que eu nao esperava, as threads foram executadas porem nenhum dado foi salvo na base de dados, alguem sabe me dizer o que pode ter acontecido?
Attt,