Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.{SocketException, UnknownHostException, SocketTimeoutException}
- import java.util.concurrent.{TimeUnit, ExecutorService, Executors}
- import scalaj.http.Http
- import java.io._
- import java.sql._
- object Main extends App {
- val sites = "E:\\Tools\\scala\\sites.txt"
- val poolSize = 1000
- val pool: ExecutorService = Executors.newFixedThreadPool(poolSize)
- val myDriver = "com.mysql.jdbc.Driver"
- val myUrl = "jdbc:mysql://localhost/mydb"
- var conn: Connection = null
- Class.forName(myDriver)
- conn = DriverManager.getConnection(myUrl, "root", "t,@ysq1")
- val now = System.nanoTime
- for (line <- scala.io.Source.fromFile(sites).getLines()) {
- pool.execute(new Fetcher(line, conn))
- }
- pool.shutdown()
- try {
- pool.awaitTermination(Long.MaxValue, TimeUnit.NANOSECONDS)
- val elapsedTime = System.nanoTime - now
- println("Time elapsed: " + elapsedTime / 1000000000.0 + " seconds")
- } catch {
- case e: InterruptedException =>
- println("process interrupted")
- }
- conn.close()
- }
- class Fetcher(url: String, conn: Connection) extends Runnable {
- def run() {
- val writer1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("socket_error.txt", true), "UTF-8"))
- val writer2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("socket_timeout.txt", true), "UTF-8"))
- val writer3 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("unknown_host.txt", true), "UTF-8"))
- println("Fetching " + url)
- try {
- val res = Http("http://" + url).timeout(connTimeoutMs = 10000, readTimeoutMs = 5000).asString
- println(url + " fetched")
- val query = "INSERT INTO test (site, data) values (?, ?)"
- val preparedStmt = conn.prepareStatement(query)
- preparedStmt.setString(1, url)
- preparedStmt.setString(2, res.toString())
- preparedStmt.execute();
- } catch {
- case se: SocketException =>
- println("---!---" + url + " socket error")
- writer1.write(url + '\n')
- case ste: SocketTimeoutException =>
- println("---!---" + url + " socket timeout")
- writer2.write(url + '\n')
- case uhe: UnknownHostException =>
- println("---!---" + url + " unknown host")
- writer3.write(url + '\n')
- }
- writer1.close()
- writer2.close()
- writer3.close()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement