Autor Tema: [IMP] Automatic cleaner for inactive characters  (Leído 3118 veces)

Mayo 22, 2016, 09:08:28 pm

Desconectado swarlog

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 1.598
  • Karma: 4
  • Sexo: Masculino
    • U3Games
Simple script de limpieza automatica de jugadores inactivos, tiempo configurable desde la misma clase.

Código: You are not allowed to view links. Register or Login
/*
 * Copyright (C) 2004-2016 L2J DataPack
 *
 * This file is part of L2J DataPack.
 *
 * L2J DataPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * L2J DataPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package cron;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;

/**
 * @author Sacrifice
 */
public final class ScheduledAutoCleanCharacters
{
private static final short MAX_STORED_DAYS = 45; // By default is 45 days
private static final Logger _log = Logger.getLogger(ScheduledAutoCleanCharacters.class.getName());

public static void main(String[] args)
{
try (Connection con = ConnectionFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM characters WHERE lastAccess <= ? AND accessLevel = 0 AND online = 0"))
{
ps.setLong(1, System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
String sdf = new SimpleDateFormat("EEE MMM dd, yyyy' at 'HH:mm:ss").format(System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
_log.info("Inactive players Deleted from: " + sdf);
}
}

Código: You are not allowed to view links. Register or Login
INSERT INTO `global_tasks` VALUES ('101', 'script', 'TYPE_GLOBAL_TASK', '0', '1', '04:00:00', 'ScheduledAutoCleanCharacters.java');


Actualización de You are not allowed to view links. Register or Login:

Actualizado con SLF4J Logger.
Código: You are not allowed to view links. Register or Login
/*
 * Copyright (C) 2004-2016 L2J DataPack
 *
 * This file is part of L2J DataPack.
 *
 * L2J DataPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * L2J DataPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package cron;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;

/**
 * @author Sacrifice
 */
public final class ScheduledAutoCleanCharacters
{
private static final short MAX_STORED_DAYS = 45; // By default is 45 days
private static final Logger _log = LoggerFactory.getLogger(ScheduledAutoCleanCharacters.class.getName());

public static void main(String[] args)
{
try (Connection con = ConnectionFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM characters WHERE lastAccess <= ? AND accessLevel = 0 AND online = 0"))
{
ps.setLong(1, System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
String sdf = new SimpleDateFormat("EEE MMM dd, yyyy' at 'HH:mm:ss").format(System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
_log.info("Inactive players Deleted from: {}", sdf);
}
}
Código: You are not allowed to view links. Register or Login
INSERT INTO `global_tasks` VALUES ('101', 'script', 'TYPE_GLOBAL_TASK', '0', '1', '04:00:00', 'ScheduledAutoCleanCharacters.java');

Mayo 24, 2016, 01:21:15 am
Respuesta #1

Desconectado Kryspo

  • Usuario
  • *
  • Mensajes: 149
  • Karma: 0
  • Sexo: Masculino
  • Desarrollador Java de L2jChile
Interesante !!.... A Implementar :D

Junio 07, 2016, 12:00:15 pm
Respuesta #2

Desconectado swarlog

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 1.598
  • Karma: 4
  • Sexo: Masculino
    • U3Games
Código actualizado ^^

Junio 09, 2016, 12:58:33 pm
Respuesta #3

Desconectado Kryspo

  • Usuario
  • *
  • Mensajes: 149
  • Karma: 0
  • Sexo: Masculino
  • Desarrollador Java de L2jChile
You are not allowed to view links. Register or Login
Código actualizado ^^

Que se actualizó bro?? Por lo que me fijé, se achico un poco más el código xD

Octubre 06, 2016, 08:29:43 pm
Respuesta #4

Desconectado Sacrifice

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 194
  • Karma: 1
  • Sexo: Masculino
  • Retail GameMaster and Developer
Os gustó, eh!. Muy útil y además, configurable.
Retail GameMaster and Developer using Eclipse for Java Developers 2018-12 (64 bits), OpenJDK11 (64 bits), MySQL 8.0+ (64 bits).
GitLab You are not allowed to view links. Register or Login

Octubre 07, 2016, 03:45:41 pm
Respuesta #5

Desconectado swarlog

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 1.598
  • Karma: 4
  • Sexo: Masculino
    • U3Games
You are not allowed to view links. Register or Login
Os gustó, eh!. Muy útil y además, configurable.

Muy bueno el aporte :D

Octubre 18, 2016, 02:17:44 pm
Respuesta #6

Desconectado Sacrifice

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 194
  • Karma: 1
  • Sexo: Masculino
  • Retail GameMaster and Developer
Actualizado con SLF4J Logger.
Código: You are not allowed to view links. Register or Login
/*
 * Copyright (C) 2004-2016 L2J DataPack
 *
 * This file is part of L2J DataPack.
 *
 * L2J DataPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * L2J DataPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package cron;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;

/**
 * @author Sacrifice
 */
public final class ScheduledAutoCleanCharacters
{
private static final short MAX_STORED_DAYS = 45; // By default is 45 days
private static final Logger _log = LoggerFactory.getLogger(ScheduledAutoCleanCharacters.class.getName());

public static void main(String[] args)
{
try (Connection con = ConnectionFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM characters WHERE lastAccess <= ? AND accessLevel = 0 AND online = 0"))
{
ps.setLong(1, System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
String sdf = new SimpleDateFormat("EEE MMM dd, yyyy' at 'HH:mm:ss").format(System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
_log.info("Inactive players Deleted from: {}", sdf);
}
}
Código: You are not allowed to view links. Register or Login
INSERT INTO `global_tasks` VALUES ('101', 'script', 'TYPE_GLOBAL_TASK', '0', '1', '04:00:00', 'ScheduledAutoCleanCharacters.java');
Retail GameMaster and Developer using Eclipse for Java Developers 2018-12 (64 bits), OpenJDK11 (64 bits), MySQL 8.0+ (64 bits).
GitLab You are not allowed to view links. Register or Login

Octubre 19, 2016, 07:28:29 pm
Respuesta #7

Desconectado swarlog

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 1.598
  • Karma: 4
  • Sexo: Masculino
    • U3Games
You are not allowed to view links. Register or Login
Actualizado con SLF4J Logger.
Código: You are not allowed to view links. Register or Login
/*
 * Copyright (C) 2004-2016 L2J DataPack
 *
 * This file is part of L2J DataPack.
 *
 * L2J DataPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * L2J DataPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package cron;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;

/**
 * @author Sacrifice
 */
public final class ScheduledAutoCleanCharacters
{
private static final short MAX_STORED_DAYS = 45; // By default is 45 days
private static final Logger _log = LoggerFactory.getLogger(ScheduledAutoCleanCharacters.class.getName());

public static void main(String[] args)
{
try (Connection con = ConnectionFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM characters WHERE lastAccess <= ? AND accessLevel = 0 AND online = 0"))
{
ps.setLong(1, System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
String sdf = new SimpleDateFormat("EEE MMM dd, yyyy' at 'HH:mm:ss").format(System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
_log.info("Inactive players Deleted from: {}", sdf);
}
}
Código: You are not allowed to view links. Register or Login
INSERT INTO `global_tasks` VALUES ('101', 'script', 'TYPE_GLOBAL_TASK', '0', '1', '04:00:00', 'ScheduledAutoCleanCharacters.java');

Gracias Sacrifice, actualizo el primer post ^^

Octubre 26, 2016, 03:35:21 pm
Respuesta #8

Desconectado Sacrifice

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 194
  • Karma: 1
  • Sexo: Masculino
  • Retail GameMaster and Developer
Lo mismito que el script de auto reinicio, he "limpiado / mejorado / colocado" o como ustedes lo quieran llamar, el script sobre el tema del asunto. En sí no hay muchos cambios, pero ya saben que soy "un clasista" ;)

Código: You are not allowed to view links. Register or Login
/*
 * Copyright (C) 2004-2016 L2J DataPack
 *
 * This file is part of L2J DataPack.
 *
 * L2J DataPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * L2J DataPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package cron;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
/**
 * CRON Script for schedule an automatic clean for inactive players from global_task.
 * @author Sacrifice
 */
public final class ScheduledAutoCleanCharacters
{
private static final short MAX_STORED_DAYS = 45; // By default is 45 days
private static final Logger _log = LoggerFactory.getLogger(ScheduledAutoCleanCharacters.class.getName());

public static void main(String[] args)
{
try (Connection con = ConnectionFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM characters WHERE lastAccess <= ? AND accessLevel = 0 AND online = 0"))
{
ps.setLong(1, System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
ps.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
}
String sdf = new SimpleDateFormat("EEE MMM dd, yyyy' at 'HH:mm:ss").format(System.currentTimeMillis() - (MAX_STORED_DAYS * 24L * 60L * 60L * 1000L));
_log.info("Inactive players deleted from: {}", sdf);
}
}
Ah!, por cierto, la query de arriba (el código SQL), no hace falta ejecutarlo otra vez, vale el de arriba.
Un saludo.
Retail GameMaster and Developer using Eclipse for Java Developers 2018-12 (64 bits), OpenJDK11 (64 bits), MySQL 8.0+ (64 bits).
GitLab You are not allowed to view links. Register or Login

Octubre 26, 2016, 05:31:33 pm
Respuesta #9

Desconectado fissban

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 33
  • Karma: 0
  • Sexo: Masculino
    • l2devsadmins
ps.close();
Esto se puede borrar ya q realmente no es necesario en este caso, una ves terminada la ejecucion del contenido del try el mismo se encarga de cerrar todas las consultas echas dentro de los parentesis del mismo.

Octubre 26, 2016, 06:05:09 pm
Respuesta #10

Desconectado Sacrifice

  • Desarrollador
  • Usuario
  • *
  • Mensajes: 194
  • Karma: 1
  • Sexo: Masculino
  • Retail GameMaster and Developer
You are not allowed to view links. Register or Login
ps.close();
Esto se puede borrar ya q realmente no es necesario en este caso, una ves terminada la ejecucion del contenido del try el mismo se encarga de cerrar todas las consultas echas dentro de los parentesis del mismo.
Es verdad, ya lo he quitado. Gracias.
Retail GameMaster and Developer using Eclipse for Java Developers 2018-12 (64 bits), OpenJDK11 (64 bits), MySQL 8.0+ (64 bits).
GitLab You are not allowed to view links. Register or Login