Runnable single-thread solution for KZ prices

This commit is contained in:
2024-01-28 23:51:15 +03:00
parent 787490db33
commit ac8dbe05db
5 changed files with 137 additions and 27 deletions

View File

@ -12,6 +12,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
@ -55,8 +57,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>

View File

@ -0,0 +1,59 @@
package org.example;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPooled;
import java.io.IOException;
import java.security.GeneralSecurityException;
@Service
public class LoadGameService
{
@Autowired
private RedisService redisService;
public void loadGamesbySingleThread() throws InterruptedException
{
try (JedisPooled jedis = new JedisPooled(new HostAndPort(SteamConstants.REDIS_HOST, 6380), redisService.prepareRedisConfig()))
{
JsonNode json;
int i = 0;
while (i < 150000)
{
try
{
json = redisService.loadGameJson(String.format(SteamConstants.GAME_DETAILS_API_TEMPLATE, i, "KZ"));
}
catch (IOException e)
{
//TODO: add pushing logs to kibana or etc..
e.printStackTrace();
if (e.getMessage().equals("429"))
{
Thread.sleep(30000);
continue;
}
i++;
continue;
}
//TODO: add filter for successful and unsuccessful requests
jedis.set(String.valueOf(i), json.toString());
System.out.println("Current id = " + i);
System.out.println(json.toPrettyString());
i++;
}
}
catch (GeneralSecurityException | IOException ex)
{
//TODO: add pushing logs to kibana or etc..
ex.printStackTrace();
}
}
}

View File

@ -12,16 +12,29 @@ import java.io.IOException;
public class LoadGameTask implements Runnable
{
@Autowired
private RedisServiceImpl redisService;
private RedisService redisService;
@Override
public void run()
{
JsonNode json;
try (JedisPooled jedis = new JedisPooled(new HostAndPort(SteamConstants.REDIS_HOST, 6380), redisService.prepareRedisConfig()))
int i = 0;
while (true)
{
for (int i = 0; i < 150000; i++)
if (SteamPriceParser.gameCodeList.get(i) == null)
{
break;
}
if (!SteamPriceParser.gameCodeList.get(i))
{
//Block item
SteamPriceParser.gameCodeList.put(i, true);
break;
}
}
try (JedisPooled jedis = new JedisPooled(new HostAndPort(SteamConstants.REDIS_HOST, 6380), redisService.prepareRedisConfig()))
{
try
{
@ -31,13 +44,16 @@ public class LoadGameTask implements Runnable
{
//TODO: add pushing logs to kibana or etc..
e.printStackTrace();
continue;
unblockItem(i);
return;
}
//TODO: add filter for successful and unsuccessful requests
jedis.set(String.valueOf(i), json.toString());
System.out.println("Current id = " + i);
System.out.println(json.toPrettyString());
}
removeItem(i);
}
catch (Exception ex)
{
@ -45,4 +61,14 @@ public class LoadGameTask implements Runnable
ex.printStackTrace();
}
}
private synchronized void unblockItem(int i)
{
SteamPriceParser.gameCodeList.put(i, false);
}
private synchronized void removeItem(int i)
{
SteamPriceParser.gameCodeList.remove(i);
}
}

View File

@ -20,7 +20,7 @@ import java.security.GeneralSecurityException;
import java.security.KeyStore;
@Service
public class RedisServiceImpl
public class RedisService
{
//TODO: remove deprecated URL constructor
//TODO: replace mapper towards concrete class model GameInfoResponse
@ -38,7 +38,15 @@ public class RedisServiceImpl
};
Authenticator.setDefault(authenticator);
URLConnection connection = new URL(url).openConnection(proxy);
try
{
inputStream = connection.getInputStream();
}
catch (IOException exception)
{
int statusCode = ((HttpURLConnection)connection).getResponseCode();
throw new IOException(String.valueOf(statusCode));
}
InputStreamReader reader = new InputStreamReader(inputStream);
ObjectMapper mapper = new ObjectMapper();

View File

@ -1,9 +1,11 @@
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -11,15 +13,28 @@ import java.util.concurrent.Executors;
@ComponentScan
public class SteamPriceParser
{
public static void main(String[] args)
public static Map<Integer, Boolean> gameCodeList = new HashMap<>();
static
{
for (int i = 0; i < 150000; i++)
{
gameCodeList.put(i, false);
}
}
public static void main(String[] args) throws InterruptedException
{
// AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SteamPriceParser.class);
// ExecutorService executorService = Executors.newFixedThreadPool(50);
//
// // Hack for adding runnable to spring watching list
// LoadGameTask task = applicationContext.getBean(LoadGameTask.class);
// executorService.submit(task);
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SteamPriceParser.class);
ExecutorService executorService = Executors.newFixedThreadPool(50);
// Hack for adding runnable to spring watching list
LoadGameTask task = applicationContext.getBean(LoadGameTask.class);
executorService.submit(task);
LoadGameService loadGameService = applicationContext.getBean(LoadGameService.class);
loadGameService.loadGamesbySingleThread();
}
}