Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.sbs.goodcrawler.urlmanager; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sbs.goodcrawler.conf.GlobalConstants; import org.sbs.goodcrawler.conf.PropertyConfigurationHelper; import org.sbs.goodcrawler.exception.QueueException; /** * @author shenbaise(shenbaise@outlook.com) * @date 2013-6-29 * @desc ?Urls */ public class PendingUrls implements Serializable { private static final long serialVersionUID = 2260258894389085989L; private Log log = LogFactory.getLog(this.getClass()); private BlockingQueue<WebURL> Queue = null; private static PendingUrls instance = null; /** * URL?+1 */ private AtomicLong urlCount = new AtomicLong(0L); /** * ???URL */ private AtomicLong success = new AtomicLong(0L); /** * ?URL */ private AtomicInteger failure = new AtomicInteger(0); /** * ??job? */ private AtomicLong ignored = new AtomicLong(0); /** * */ private PendingUrls() { init(); } /** * @return * @desc */ public static PendingUrls getInstance() { if (null == instance) { instance = new PendingUrls(); } return instance; } /** * @desc ? */ private void init() { File file = new File(PropertyConfigurationHelper.getInstance().getString("status.save.path", "status") + File.separator + "urls.good"); if (file.exists()) { try { FileInputStream fisUrl = new FileInputStream(file); ObjectInputStream oisUrl = new ObjectInputStream(fisUrl); instance = (PendingUrls) oisUrl.readObject(); oisUrl.close(); fisUrl.close(); Queue = instance.Queue; failure = instance.failure; success = instance.success; urlCount = instance.urlCount; ignored = instance.ignored; System.out.println("recovery url queue..." + Queue.size()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } if (null == Queue) Queue = new ArrayBlockingQueue<>(PropertyConfigurationHelper.getInstance() .getInt(GlobalConstants.pendingUrlsQueueSize, 1000000)); } /** * @param url * @desc ?URLUrl+1 */ public void addUrl(WebURL url) throws QueueException { try { if (null != url) { Queue.put(url); urlCount.getAndIncrement(); } } catch (InterruptedException e) { log.error(e.getMessage()); throw new QueueException("??"); } } /** * ?URLUrl+1Url * @param url * @param timeout (MILLISECONDS) * @return * @throws QueueException */ public boolean addUrl(WebURL url, int timeout) throws QueueException { if (url != null) { try { boolean b = Queue.offer(url, timeout, TimeUnit.MILLISECONDS); if (b) { urlCount.getAndIncrement(); } return b; } catch (InterruptedException e) { throw new QueueException("???"); } } return false; } /** * @return * @desc ??URL */ public WebURL getUrl() throws QueueException { try { return Queue.take(); } catch (InterruptedException e) { log.error(e.getMessage()); throw new QueueException("???"); } } public boolean isEmpty() { return Queue.isEmpty(); } /** * @param c * @return * @desc ??+c */ public long processedSuccess(long c) { return success.addAndGet(c); } /** * @return * @desc ??+1 */ public long processedSuccess() { return success.incrementAndGet(); } /** * @param c * @return * @desc ?+c */ public int processedFailure(int c) { return failure.addAndGet(c); } /** * @return * @desc ?+1 */ public int processedFailure() { return failure.incrementAndGet(); } /** * @return * @desc */ public long urlCount() { return urlCount.get(); } /** * @return * @desc ?? */ public long success() { return success.get(); } /** * @return * @desc ? */ public int failure() { return failure.get(); } /** * @param c * @return * @desc +c */ public long processedIgnored(long c) { return ignored.addAndGet(c); } /** * @return * @desc +1 */ public long processedIgnored() { return ignored.incrementAndGet(); } /** * @return * @desc */ public long ignored() { return ignored.get(); } public String pendingStatus() { StringBuilder sb = new StringBuilder(32); sb.append("?URL").append(Queue.size()).append("") .append("?").append(urlCount).append("??") .append(success.get()).append("").append(failure.get()).append("") .append(ignored()).append(""); return sb.toString(); } }