Advertisement
Guest User

Untitled

a guest
Feb 9th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.45 KB | None | 0 0
  1. import java.io._
  2. import java.net.{SocketException, SocketTimeoutException, UnknownHostException}
  3. import java.util.concurrent.{ExecutorService, Executors, TimeUnit}
  4.  
  5. import org.apache.commons.dbcp2._
  6.  
  7. import scalaj.http.Http
  8.  
  9. object Main extends App {
  10.  
  11.   val sites = "E:\\Tools\\scala\\sites.txt"
  12.   val poolSize = 1000
  13.  
  14.   val pool: ExecutorService = Executors.newFixedThreadPool(poolSize)
  15.  
  16.   val now = System.nanoTime
  17.   for (line <- scala.io.Source.fromFile(sites).getLines()) {
  18.     pool.execute(new Fetcher(line))
  19.   }
  20.   pool.shutdown()
  21.   try {
  22.     pool.awaitTermination(Long.MaxValue, TimeUnit.NANOSECONDS)
  23.     val elapsedTime = System.nanoTime - now
  24.     println("Time elapsed: " + elapsedTime / 1000000000.0 + " seconds")
  25.   } catch {
  26.     case e: InterruptedException =>
  27.       println("process interrupted")
  28.   }
  29. }
  30.  
  31. class Fetcher(url: String) extends Runnable {
  32.  
  33.   def run() {
  34.     val writer1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("socket_error.txt", true), "UTF-8"))
  35.     val writer2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("socket_timeout.txt", true), "UTF-8"))
  36.     val writer3 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("unknown_host.txt", true), "UTF-8"))
  37.  
  38.     println("Fetching " + url)
  39.     try {
  40.       val res = Http("http://" + url).timeout(connTimeoutMs = 10000, readTimeoutMs = 5000).asString
  41.       println(url + " fetched")
  42.       val conn = Datasource.connectionPool.getConnection
  43.       val query = "INSERT INTO test (site, data) values (?, ?)"
  44.       val preparedStmt = conn.prepareStatement(query)
  45.       preparedStmt.setString(1, url)
  46.       preparedStmt.setString(2, res.toString)
  47.       preparedStmt.execute()
  48.     } catch {
  49.       case se: SocketException =>
  50.         println("---!---" + url + " socket error")
  51.         writer1.write(url + '\n')
  52.       case ste: SocketTimeoutException =>
  53.         println("---!---" + url + " socket timeout")
  54.         writer2.write(url + '\n')
  55.       case uhe: UnknownHostException =>
  56.         println("---!---" + url + " unknown host")
  57.         writer3.write(url + '\n')
  58.     }
  59.     writer1.close()
  60.     writer2.close()
  61.     writer3.close()
  62.   }
  63. }
  64.  
  65. object Datasource {
  66.   val connectionPool = new BasicDataSource()
  67.   connectionPool.setDriverClassName("com.mysql.jdbc.Driver")
  68.   connectionPool.setUrl("jdbc:mysql://localhost/mydb")
  69.   connectionPool.setUsername("root")
  70.   connectionPool.setPassword("t,@ysq1")
  71.   connectionPool.setInitialSize(3)
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement