List of usage examples for java.util.concurrent ThreadPoolExecutor execute
public void execute(Runnable command)
From source file:org.apache.hama.graph.GraphJobRunner.java
/** * Loads vertices into memory of each peer. */// ww w. j a v a 2s . c om @SuppressWarnings("unchecked") private void loadVertices(BSPPeer<Writable, Writable, Writable, Writable, GraphJobMessage> peer) throws IOException, SyncException, InterruptedException { for (int i = 0; i < peer.getNumPeers(); i++) { partitionMessages.put(i, new GraphJobMessage()); } VertexInputReader<Writable, Writable, V, E, M> reader = (VertexInputReader<Writable, Writable, V, E, M>) ReflectionUtils .newInstance(conf.getClass(Constants.RUNTIME_PARTITION_RECORDCONVERTER, VertexInputReader.class)); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); executor.setMaximumPoolSize(conf.getInt(DEFAULT_THREAD_POOL_SIZE, 64)); executor.setRejectedExecutionHandler(retryHandler); KeyValuePair<Writable, Writable> next = null; while ((next = peer.readNext()) != null) { Vertex<V, E, M> vertex = GraphJobRunner.<V, E, M>newVertexInstance(VERTEX_CLASS); boolean vertexFinished = false; try { vertexFinished = reader.parseVertex(next.getKey(), next.getValue(), vertex); } catch (Exception e) { throw new IOException("Parse exception occured: " + e); } if (!vertexFinished) { continue; } Runnable worker = new Parser(vertex); executor.execute(worker); } executor.shutdown(); executor.awaitTermination(60, TimeUnit.SECONDS); Iterator<Entry<Integer, GraphJobMessage>> it; it = partitionMessages.entrySet().iterator(); while (it.hasNext()) { Entry<Integer, GraphJobMessage> e = it.next(); it.remove(); GraphJobMessage msg = e.getValue(); msg.setFlag(GraphJobMessage.PARTITION_FLAG); peer.send(getHostName(e.getKey()), msg); } peer.sync(); executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); executor.setMaximumPoolSize(conf.getInt(DEFAULT_THREAD_POOL_SIZE, 64)); executor.setRejectedExecutionHandler(retryHandler); GraphJobMessage msg; while ((msg = peer.getCurrentMessage()) != null) { executor.execute(new AddVertex(msg)); } executor.shutdown(); executor.awaitTermination(60, TimeUnit.SECONDS); LOG.info(vertices.size() + " vertices are loaded into " + peer.getPeerName()); }
From source file:org.apache.hc.core5.http.benchmark.HttpBenchmark.java
public Results doExecute() throws Exception { final URL url = config.getUrl(); final long endTime = System.currentTimeMillis() + config.getTimeLimit() * 1000; final HttpHost host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); final ThreadPoolExecutor workerPool = new ThreadPoolExecutor(config.getThreads(), config.getThreads(), 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { @Override//ww w. j ava2 s . c om public Thread newThread(final Runnable r) { return new Thread(r, "ClientPool"); } }); workerPool.prestartAllCoreThreads(); SocketFactory socketFactory = null; if ("https".equals(host.getSchemeName())) { final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.setProtocol("SSL"); if (config.isDisableSSLVerification()) { sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }); } else if (config.getTrustStorePath() != null) { sslContextBuilder.loadTrustMaterial(new File(config.getTrustStorePath()), config.getTrustStorePassword() != null ? config.getTrustStorePassword().toCharArray() : null); } if (config.getIdentityStorePath() != null) { sslContextBuilder.loadKeyMaterial(new File(config.getIdentityStorePath()), config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null, config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null); } final SSLContext sslContext = sslContextBuilder.build(); socketFactory = sslContext.getSocketFactory(); } final BenchmarkWorker[] workers = new BenchmarkWorker[config.getThreads()]; for (int i = 0; i < workers.length; i++) { workers[i] = new BenchmarkWorker(host, createRequest(host), socketFactory, config); workerPool.execute(workers[i]); } while (workerPool.getCompletedTaskCount() < config.getThreads()) { Thread.yield(); try { Thread.sleep(1000); } catch (final InterruptedException ignore) { } if (config.getTimeLimit() != -1 && System.currentTimeMillis() > endTime) { for (int i = 0; i < workers.length; i++) { workers[i].setShutdownSignal(); } } } workerPool.shutdown(); return ResultProcessor.collectResults(workers, host, config.getUrl().toString()); }
From source file:org.apache.http.benchmark.HttpBenchmark.java
public String execute() throws Exception { prepare();//from w ww .j ava 2 s . com ThreadPoolExecutor workerPool = new ThreadPoolExecutor(config.getThreads(), config.getThreads(), 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(r, "ClientPool"); } }); workerPool.prestartAllCoreThreads(); BenchmarkWorker[] workers = new BenchmarkWorker[config.getThreads()]; for (int i = 0; i < workers.length; i++) { workers[i] = new BenchmarkWorker(params, config.getVerbosity(), request[i], host, config.getRequests(), config.isKeepAlive(), config.isDisableSSLVerification(), config.getTrustStorePath(), config.getTrustStorePassword(), config.getIdentityStorePath(), config.getIdentityStorePassword()); workerPool.execute(workers[i]); } while (workerPool.getCompletedTaskCount() < config.getThreads()) { Thread.yield(); try { Thread.sleep(1000); } catch (InterruptedException ignore) { } } workerPool.shutdown(); return ResultProcessor.printResults(workers, host, config.getUrl().toString(), contentLength); }
From source file:org.apache.http.contrib.benchmark.HttpBenchmark.java
private void execute() { prepare();// w ww. j a v a 2 s .c o m ThreadPoolExecutor workerPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(r, "ClientPool"); } }); workerPool.prestartAllCoreThreads(); BenchmarkWorker[] workers = new BenchmarkWorker[threads]; for (int i = 0; i < threads; i++) { workers[i] = new BenchmarkWorker(params, verbosity, request[i], host, requests, keepAlive); workerPool.execute(workers[i]); } while (workerPool.getCompletedTaskCount() < threads) { Thread.yield(); try { Thread.sleep(1000); } catch (InterruptedException ignore) { } } workerPool.shutdown(); ResultProcessor.printResults(workers, host, url.toString(), contentLength); }
From source file:org.apache.solr.cloud.BasicDistributedZkTest.java
protected void createCores(final HttpSolrClient client, ThreadPoolExecutor executor, final String collection, final int numShards, int cnt) { for (int i = 0; i < cnt; i++) { final int freezeI = i; executor.execute(() -> { Create createCmd = new Create(); createCmd.setCoreName(collection + freezeI); createCmd.setCollection(collection); createCmd.setNumShards(numShards); try { String core3dataDir = createTempDir(collection).toFile().getAbsolutePath(); createCmd.setDataDir(getDataDir(core3dataDir)); client.request(createCmd); } catch (SolrServerException | IOException e) { throw new RuntimeException(e); }//from ww w . j a va 2 s . c om }); } }
From source file:org.bimserver.tests.TestSimultaniousDownloadWithCaching.java
private void start() { BimServerConfig config = new BimServerConfig(); Path homeDir = Paths.get("home"); try {//from w w w . j a v a 2 s. co m if (Files.isDirectory(homeDir)) { PathUtils.removeDirectoryWithContent(homeDir); } } catch (IOException e) { e.printStackTrace(); } config.setClassPath(System.getProperty("java.class.path")); config.setHomeDir(homeDir); config.setPort(8080); config.setStartEmbeddedWebServer(true); config.setResourceFetcher(new LocalDevelopmentResourceFetcher(Paths.get("../"))); final BimServer bimServer = new BimServer(config); try { LocalDevPluginLoader.loadPlugins(bimServer.getPluginManager(), null); bimServer.start(); if (bimServer.getServerInfo().getServerState() == ServerState.NOT_SETUP) { bimServer.getService(AdminInterface.class).setup("http://localhost", "Administrator", "admin@bimserver.org", "admin", null, null, null); } } catch (PluginException e2) { e2.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (DatabaseInitException e) { e.printStackTrace(); } catch (BimserverDatabaseException e) { e.printStackTrace(); } catch (DatabaseRestartRequiredException e) { e.printStackTrace(); } catch (UserException e) { e.printStackTrace(); } try { final ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL); ServiceInterface serviceInterface = serviceMap.get(ServiceInterface.class); SettingsInterface settingsInterface = serviceMap.get(SettingsInterface.class); final AuthInterface authInterface = serviceMap.get(AuthInterface.class); serviceInterface = bimServer.getServiceFactory() .get(authInterface.login("admin@bimserver.org", "admin"), AccessMethod.INTERNAL) .get(ServiceInterface.class); settingsInterface.setCacheOutputFiles(true); settingsInterface.setGenerateGeometryOnCheckin(false); final SProject project = serviceMap.getServiceInterface().addProject("test", "ifc2x3tc1"); SDeserializerPluginConfiguration deserializerByName = serviceMap.getServiceInterface() .getDeserializerByName("IfcStepDeserializer"); Path file = Paths.get("../TestData/data/AC11-Institute-Var-2-IFC.ifc"); serviceInterface.checkin(project.getOid(), "test", deserializerByName.getOid(), file.toFile().length(), file.getFileName().toString(), new DataHandler(new FileDataSource(file.toFile())), false, true); final SProject projectUpdate = serviceMap.getServiceInterface().getProjectByPoid(project.getOid()); ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 20, 1, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(1000)); for (int i = 0; i < 20; i++) { executor.execute(new Runnable() { @Override public void run() { try { ServiceMap serviceMap2 = bimServer.getServiceFactory().get( authInterface.login("admin@bimserver.org", "admin"), AccessMethod.INTERNAL); SSerializerPluginConfiguration serializerPluginConfiguration = serviceMap .getServiceInterface().getSerializerByName("Ifc2x3"); Long download = serviceMap2.getServiceInterface().download( Collections.singleton(projectUpdate.getLastRevisionId()), DefaultQueries.allAsString(), serializerPluginConfiguration.getOid(), true); SDownloadResult downloadData = serviceMap2.getServiceInterface() .getDownloadData(download); if (downloadData.getFile() .getDataSource() instanceof CacheStoringEmfSerializerDataSource) { CacheStoringEmfSerializerDataSource c = (CacheStoringEmfSerializerDataSource) downloadData .getFile().getDataSource(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); c.writeToOutputStream(baos, null); System.out.println(baos.size()); } catch (SerializerException e) { e.printStackTrace(); } } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(downloadData.getFile().getInputStream(), baos); System.out.println(baos.size()); } serviceMap2.getServiceInterface().cleanupLongAction(download); } catch (ServerException e) { e.printStackTrace(); } catch (UserException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (PublicInterfaceNotFoundException e1) { e1.printStackTrace(); } } }); } executor.shutdown(); executor.awaitTermination(1, TimeUnit.HOURS); bimServer.stop(); } catch (ServerException e1) { e1.printStackTrace(); } catch (UserException e1) { e1.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (PublicInterfaceNotFoundException e2) { e2.printStackTrace(); } }
From source file:org.cesecore.audit.log.LogPerformanceTest.java
@Test public void testSecureLog() throws Exception { log.trace(">testSecureLog"); // Use 1024 SHA512withRSA logManagement.changeLogManagement(roleMgmgToken, createDigSignLogManagementData()); // Export (delete) any old log first final CryptoToken cryptoToken = createTokenWithKeyPair(); for (final String logDeviceId : securityEventsAuditor.getQuerySupportingLogDevices()) { final String exportFilename = securityEventsAuditor.exportAuditLogs(roleMgmgToken, cryptoToken, new Date(), true, keyAlias, keyPairSignAlgorithm, logDeviceId).getExportedFile(); assertExportAndSignatureExists(exportFilename); }/* ww w .j a v a 2s.c o m*/ // Log some and measure time final ThreadPoolExecutor workers = (ThreadPoolExecutor) Executors.newFixedThreadPool(THREADS); final long startTimeLog = System.currentTimeMillis(); for (int i = 0; i < WORKERS; i++) { workers.execute(new Runnable() { @Override public void run() { try { securityEventsLogger.log(roleMgmgToken, EventTypes.AUTHENTICATION, EventStatus.SUCCESS, ModuleTypes.AUTHENTICATION, ServiceTypes.CORE); } catch (Exception e) { fail("Logging should work"); } } }); } while (workers.getCompletedTaskCount() < WORKERS && (System.currentTimeMillis() - startTimeLog) < TIMEOUT_MS) { Thread.sleep(250); } final int completedTaskCount = Long.valueOf(workers.getCompletedTaskCount()).intValue(); log.info("securityEventsLogger.log: " + completedTaskCount + " completed in " + (System.currentTimeMillis() - startTimeLog) + " ms using " + THREADS + " threads."); workers.shutdown(); for (final String logDeviceId : securityEventsAuditor.getQuerySupportingLogDevices()) { log.info("using device: " + logDeviceId); // Validate final QueryCriteria criteria = QueryCriteria.create() .add(Criteria.orderDesc(AuditLogEntry.FIELD_TIMESTAMP)); final List<? extends AuditLogEntry> list = securityEventsAuditor.selectAuditLogs(roleMgmgToken, 1, completedTaskCount, criteria, logDeviceId); assertEquals(list.size(), completedTaskCount); final long startTimeVerify = System.currentTimeMillis(); securityEventsAuditor.verifyLogsIntegrity(roleMgmgToken, new Date(), logDeviceId); log.info("securityEventsLogger.verify: " + (System.currentTimeMillis() - startTimeVerify)); // Export final long startTimeExport = System.currentTimeMillis(); final String exportFilename = securityEventsAuditor.exportAuditLogs(roleMgmgToken, cryptoToken, new Date(), true, keyAlias, keyPairSignAlgorithm, logDeviceId).getExportedFile(); log.info("securityEventsLogger.export: " + (System.currentTimeMillis() - startTimeExport)); assertExportAndSignatureExists(exportFilename); } log.trace("<testSecureLog"); }
From source file:org.ecocean.servlet.AdoptionAction.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String adopterName = ""; String adopterAddress = ""; String adopterEmail = ""; String adopterImage;// www . j a v a 2 s . c o m String adoptionStartDate = ""; String adoptionEndDate = ""; String adopterQuote = ""; String adoptionManager = ""; String shark = ""; String encounter = ""; String notes = ""; String adoptionType = ""; String number = ""; String text = ""; // Saved to the selected shark, not the adoption. String newNickName = ""; // Storing the customer ID here makes the subscription cancellation process easier to do in less moves. String stripeCustomerID = ""; // Stores some wack response string from google recaptcha. String gresp = ""; boolean adoptionSuccess = true; String failureMessage = ""; //set UTF-8 request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(true); String context = "context0"; context = ServletUtilities.getContext(request); Shepherd myShepherd = new Shepherd(context); myShepherd.setAction("AdoptionAction.class"); System.out.println("in context " + context); //request.getSession()getServlet().getServletContext().getRealPath("/")); String rootDir = getServletContext().getRealPath("/"); System.out.println("rootDir=" + rootDir); // This value is only stored in the email specific edit form. Boolean emailEdit = false; if ((Boolean) session.getAttribute("emailEdit") != false) { emailEdit = (Boolean) session.getAttribute("emailEdit"); number = (String) session.getAttribute("sessionAdoptionID"); } //setup data dir String rootWebappPath = getServletContext().getRealPath("/"); File webappsDir = new File(rootWebappPath).getParentFile(); File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context)); //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();} File adoptionsDir = new File(shepherdDataDir.getAbsolutePath() + "/adoptions"); if (!adoptionsDir.exists()) { adoptionsDir.mkdirs(); } //get the form to read data from // AdoptionForm theForm = (AdoptionForm) form; //set up for response response.setContentType("text/html"); PrintWriter out = response.getWriter(); boolean locked = false; String fileName = "None"; String username = "None"; String fullPathFilename = ""; String id = ""; boolean fileSuccess = false; //kinda pointless now as we just build sentFiles list now at this point (do file work at end) String doneMessage = ""; List<String> filesOK = new ArrayList<String>(); HashMap<String, String> filesBad = new HashMap<String, String>(); List<FileItem> formFiles = new ArrayList<FileItem>(); Calendar date = Calendar.getInstance(); long maxSizeMB = CommonConfiguration.getMaxMediaSizeInMegabytes(context); long maxSizeBytes = maxSizeMB * 1048576; //set form value hashmap HashMap fv = new HashMap(); //else { id = "adpt" + (new Integer(date.get(Calendar.DAY_OF_MONTH))).toString() + (new Integer(date.get(Calendar.MONTH) + 1)).toString() + (new Integer(date.get(Calendar.YEAR))).toString() + (new Integer(date.get(Calendar.HOUR_OF_DAY))).toString() + (new Integer(date.get(Calendar.MINUTE))).toString() + (new Integer(date.get(Calendar.SECOND))).toString(); //} System.out.println("Starting an adoption submission..."); Calendar todayDate = Calendar.getInstance(); if (ServletFileUpload.isMultipartContent(request)) { try { ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); upload.setHeaderEncoding("UTF-8"); List<FileItem> multiparts = upload.parseRequest(request); //List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : multiparts) { if (item.isFormField()) { //plain field fv.put(item.getFieldName(), ServletUtilities.preventCrossSiteScriptingAttacks(item.getString("UTF-8").trim())); //TODO do we want trim() here??? -jon //System.out.println("got regular field (" + item.getFieldName() + ")=(" + item.getString("UTF-8") + ")"); } else { //file //System.out.println("content type???? " + item.getContentType()); TODO note, the helpers only check extension if (item.getSize() > maxSizeBytes) { filesBad.put(item.getName(), "file is larger than " + maxSizeMB + "MB"); } else if (myShepherd.isAcceptableImageFile(item.getName()) || myShepherd.isAcceptableVideoFile(item.getName())) { formFiles.add(item); filesOK.add(item.getName()); } else { filesBad.put(item.getName(), "invalid type of file"); } } } doneMessage = "File Uploaded Successfully"; fileSuccess = true; } catch (Exception ex) { doneMessage = "File Upload Failed due to " + ex; } } else { doneMessage = "Sorry this Servlet only handles file upload request"; } session.setAttribute("filesOKMessage", (filesOK.isEmpty() ? "none" : Arrays.toString(filesOK.toArray()))); String badmsg = ""; for (String key : filesBad.keySet()) { badmsg += key + " (" + getVal(filesBad, key) + ") "; } if (badmsg.equals("")) { badmsg = "none"; } session.setAttribute("filesBadMessage", badmsg); boolean isEdit = false; if (fileSuccess) { if ((fv.get("number") != null) && !fv.get("number").toString().equals("")) { //handle adoption number processing number = fv.get("number").toString(); if ((number != null) && (!number.equals(""))) { isEdit = true; System.out.println("Ping! Hit adoption number recieved by action servlet."); //myShepherd.beginDBTransaction(); } //end adoption number/id processing } if ((fv.get("adopterName") != null) && !fv.get("adopterName").toString().equals("")) { adopterName = fv.get("adopterName").toString().trim(); } if ((fv.get("adopterAddress") != null) && !fv.get("adopterAddress").toString().equals("")) { adopterAddress = fv.get("adopterAddress").toString().trim(); } if ((fv.get("adopterEmail") != null) && !fv.get("adopterEmail").toString().equals("")) { adopterEmail = fv.get("adopterEmail").toString().trim(); } if ((fv.get("adoptionStartDate") != null) && !fv.get("adoptionStartDate").toString().equals("")) { adoptionStartDate = fv.get("adoptionStartDate").toString().trim(); } if ((fv.get("adoptionEndDate") != null) && !fv.get("adoptionEndDate").toString().equals("")) { adoptionEndDate = fv.get("adoptionEndDate").toString().trim(); } if ((fv.get("adopterQuote") != null) && !fv.get("adopterQuote").toString().equals("")) { adopterQuote = fv.get("adopterQuote").toString().trim(); } if ((fv.get("adoptionManager") != null) && !fv.get("adoptionManager").toString().equals("")) { adoptionManager = fv.get("adoptionManager").toString().trim(); } if ((fv.get("shark") != null) && !fv.get("shark").toString().equals("")) { shark = fv.get("shark").toString().trim(); } if ((fv.get("encounter") != null) && !fv.get("encounter").toString().equals("")) { encounter = fv.get("encounter").toString().trim(); } if ((fv.get("notes") != null) && !fv.get("notes").toString().equals("")) { notes = fv.get("notes").toString().trim(); } if ((fv.get("adoptionType") != null) && !fv.get("adoptionType").toString().equals("")) { adoptionType = fv.get("adoptionType").toString().trim(); } if ((fv.get("text") != null) && !fv.get("text").toString().equals("")) { text = fv.get("text").toString().trim(); } // New nickname to save to marked individual object. if ((fv.get("newNickName") != null) && !fv.get("newNickName").toString().equals("")) { newNickName = fv.get("newNickName").toString().trim(); } if ((fv.get("g-recaptcha-response") != null) && !fv.get("g-recaptcha-response").toString().equals("")) { gresp = fv.get("g-recaptcha-response").toString().trim(); } if (isEdit) { id = number; } // Grab the stripe customer out of session. stripeCustomerID = (String) session.getAttribute("stripeID"); File thisAdoptionDir = new File(adoptionsDir.getAbsolutePath() + "/" + id); if (!thisAdoptionDir.exists()) { thisAdoptionDir.mkdirs(); } String baseDir = ServletUtilities.dataDir(context, rootDir); ArrayList<SinglePhotoVideo> images = new ArrayList<SinglePhotoVideo>(); for (FileItem item : formFiles) { /* this will actually write file to filesystem (or [FUTURE] wherever) TODO: either (a) undo this if any failure of writing encounter; or (b) dont write til success of enc. */ //try { //SinglePhotoVideo spv = new SinglePhotoVideo(encID, item, context, encDataDir); //SinglePhotoVideo spv = new SinglePhotoVideo(enc, item, context, baseDir); try { //retrieve the file data ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream stream = item.getInputStream(); //System.out.println(writeFile); //if ((!(file[iter].getFileName().equals(""))) && (file[iter].getFileSize() > 0)) { //write the file to the file specified //String writeName=file[iter].getFileName().replace('#', '_').replace('-', '_').replace('+', '_').replaceAll(" ", "_"); //String writeName=forHTMLTag(file[iter].getFileName()); String writeName = "adopter.jpg"; //String writeName=URLEncoder.encode(file[iter].getFileName(), "UTF-8"); //while (writeName.indexOf(".") != writeName.lastIndexOf(".")) { // writeName = writeName.replaceFirst("\\.", "_"); // } //System.out.println(writeName); OutputStream bos = new FileOutputStream(new File(thisAdoptionDir, writeName)); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); //data = "The file has been written to \"" + id + "\\" + writeName + "\""; adopterImage = writeName; // } //close the stream stream.close(); baos.close(); } catch (FileNotFoundException fnfe) { System.out.println("File not found exception.\n"); fnfe.printStackTrace(); //return null; } catch (IOException ioe) { System.out.println("IO Exception.\n"); ioe.printStackTrace(); //return null; } } // This verifies the user being logged in or passing the recapture. boolean loggedIn = false; try { if (request.getUserPrincipal() != null) { loggedIn = true; } } catch (NullPointerException ne) { System.out.println("Got a null pointer checking for logged in user."); } boolean validCaptcha = false; if (loggedIn != true) { String remoteIP = request.getRemoteAddr(); validCaptcha = ServletUtilities.captchaIsValid(context, gresp, remoteIP); System.out.println("Results from captchaIsValid(): " + validCaptcha); } if ((validCaptcha == true) || (loggedIn == true)) { System.out.println("Ping! Hit the Adoption creation section."); try { Adoption ad = new Adoption(id, adopterName, adopterEmail, adoptionStartDate, adoptionEndDate); if (isEdit || emailEdit) { ad = myShepherd.getAdoption(number); ad.setAdopterName(adopterName); ad.setAdopterEmail(adopterEmail); ad.setAdoptionEndDate(adoptionEndDate); ad.setAdoptionStartDate(adoptionStartDate); } ad.setAdopterQuote(adopterQuote); ad.setAdoptionManager(adoptionManager); ad.setIndividual(shark); ad.setEncounter(encounter); ad.setNotes(notes); ad.setAdoptionType(adoptionType); ad.setAdopterAddress(adopterAddress); ad.setStripeCustomerId(stripeCustomerID); if ((filesOK != null) && (filesOK.size() > 0)) { ad.setAdopterImage(filesOK.get(0)); } myShepherd.beginDBTransaction(); if (adoptionSuccess && !isEdit) { try { myShepherd.storeNewAdoption(ad, id); } catch (Exception e) { adoptionSuccess = false; failureMessage += "Failed to presist the new adoption.<br>"; } } // New logic to change marked individual nickname if necessary in adoption. MarkedIndividual mi = myShepherd.getMarkedIndividual(shark); if (!newNickName.equals("")) { if (adoptionSuccess && !isEdit) { try { mi.setNickName(newNickName); mi.setNickNamer(adopterName); } catch (Exception e) { failureMessage += "Retrieving shark to set nickname failed.<br>"; } } } // Sends a confirmation email to a a new adopter with cancellation and update information. if (emailEdit == false) { try { String emailContext = "context0"; String langCode = "en"; String to = ad.getAdopterEmail(); String type = "adoptionConfirmation"; System.out.println("About to email new adopter."); // Retrieve background service for processing emails ThreadPoolExecutor es = MailThreadExecutorService.getExecutorService(); Map<String, String> tagMap = NotificationMailer.createBasicTagMap(request, mi, ad); NotificationMailer mailer = new NotificationMailer(emailContext, langCode, to, type, tagMap); NotificationMailer adminMailer = new NotificationMailer(emailContext, langCode, CommonConfiguration.getNewSubmissionEmail(emailContext), type, tagMap); es.execute(mailer); es.execute(adminMailer); } catch (Exception e) { System.out.println("Error in sending email confirmation of adoption."); e.printStackTrace(); } } if ((adoptionSuccess && isEdit) || (emailEdit == true)) { myShepherd.commitDBTransaction(); } } catch (Exception e) { System.out.println("The recaptcha passed but something went wrong saving the adoption."); e.printStackTrace(); } } } // Sets adoption paid to false to allow multiple adoptions session.setAttribute("paid", false); //return a forward to display.jsp System.out.println("Ending adoption data submission."); //if((submitterID!=null)&&(submitterID.equals("deepblue"))) { if ((adoptionSuccess) && (emailEdit == false)) { response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/adoptions/adoptionSuccess.jsp?id=" + id); } else if ((adoptionSuccess) && (emailEdit == true)) { response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/adoptions/editSuccess.jsp"); } else { response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/adoptions/adoptionFailure.jsp?message=" + failureMessage); } //} myShepherd.closeDBTransaction(); }
From source file:org.entrystore.harvesting.oaipmh.jobs.ListRecordsJob.java
/** * //from w w w. j av a 2s . c o m * @param out * @throws Exception */ synchronized public void run(OutputStream out, JobExecutionContext jobContext) throws Exception { JobDataMap dataMap = jobContext.getJobDetail().getJobDataMap(); RepositoryManagerImpl rm = (RepositoryManagerImpl) dataMap.get("rm"); ContextManager cm = rm.getContextManager(); final PrincipalManager pm = rm.getPrincipalManager(); initXpath(); URI contextURI = (URI) dataMap.get("contextURI"); String contextId = contextURI.toString().substring(contextURI.toString().lastIndexOf("/") + 1); final Context context = cm.getContext(contextId); final String metadataType = dataMap.getString("metadataType"); final String target = dataMap.getString("target"); String from = dataMap.getString("from"); String until = dataMap.getString("until"); String set = dataMap.getString("set"); replaceMetadata = "replace" .equalsIgnoreCase(rm.getConfiguration().getString(Settings.HARVESTER_OAI_METADATA_POLICY, "skip")); boolean fromAutoDetect = "on" .equalsIgnoreCase(rm.getConfiguration().getString(Settings.HARVESTER_OAI_FROM_AUTO_DETECT, "on")); if (from == null && fromAutoDetect) { Date latestEntry = null; Set<URI> allEntries = context.getEntries(); for (URI uri : allEntries) { Entry entry = context.getByEntryURI(uri); if (entry != null && (EntryType.Reference.equals(entry.getEntryType()) || EntryType.LinkReference.equals(entry.getEntryType()))) { Date cachedDate = entry.getExternalMetadataCacheDate(); if (cachedDate != null) { if (latestEntry == null || cachedDate.after(latestEntry)) { latestEntry = cachedDate; } } } } if (latestEntry != null) { from = new SimpleDateFormat("yyyy-MM-dd").format(latestEntry); } } log.info("OAI-PMH metadataType: " + metadataType); log.info("OAI-PMH target: " + target); log.info("OAI-PMH from: " + from); log.info("OAI-PMH until: " + until); log.info("OAI-PMH set: " + set); // Get the listrecord from the OAI-PMH target ListRecords listRecords = null; try { listRecords = new ListRecords(target, from, until, set, metadataType); } catch (UnknownHostException e) { // TODO: handle exception write in the RDF tree log.info("UnknownHostException since the target is unknown, the havester will be deleted"); jobContext.getScheduler().interrupt(jobContext.getJobDetail().getName(), jobContext.getJobDetail().getGroup()); return; } ThreadPoolExecutor exService = null; if ("on".equalsIgnoreCase(rm.getConfiguration().getString(Settings.HARVESTER_OAI_MULTITHREADED, "off"))) { int cpuCount = Runtime.getRuntime().availableProcessors(); if (cpuCount == 1) { log.info("Multi-threaded harvesting activated, but only one CPU found; continuing single-threaded"); } else { int threadCount = cpuCount + 1; log.info("Creating executor for multi-threaded harvesting, using thread pool of " + threadCount + " (available CPUs + 1) threads"); exService = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount); } } else { log.info("Performing single-threaded harvesting"); } Date before = new Date(); int j = 0; while (listRecords != null) { NodeList errors = listRecords.getErrors(); if (errors != null && errors.getLength() > 0) { log.error("Found errors"); int length = errors.getLength(); for (int i = 0; i < length; ++i) { Node item = errors.item(i); System.out.println(item); } log.error("Error record: " + listRecords.toString()); break; } //out.write(listRecords.toString().getBytes()); // Get the <Root>-element final Element el = listRecords.getDocument().getDocumentElement(); if (el.getElementsByTagName("ListRecords").getLength() == 0) { log.error("No ListRecords"); throw new Exception("No ListRecords"); } // Get the <ListRecords> element Element listRecordsElement = (Element) el.getElementsByTagName("ListRecords").item(0); NodeList recordList = listRecordsElement.getElementsByTagName("record"); // old NodeList recordList = getRecords(listRecordsElement); // Create entries from the XML for (int i = 0; i < recordList.getLength(); i++) { final Element recordElement = (Element) recordList.item(i).cloneNode(true); if (exService == null) { try { createEntry(context, recordElement, target, metadataType); } catch (XPathExpressionException e) { log.error(e.getMessage()); } } else { exService.execute(new Runnable() { public void run() { try { pm.setAuthenticatedUserURI(pm.getAdminUser().getURI()); createEntry(context, recordElement, target, metadataType); } catch (XPathExpressionException e) { log.error(e.getMessage()); } } }); // not sure whether the following is necessary while (exService.getQueue().size() > 250) { log.info("Waiting before submitting additional Runnables, current queue size is " + exService.getQueue().size()); Thread.sleep(50); log.info("Continuing, the current queue size is " + exService.getQueue().size()); } } log.debug("total index: " + j++); } // Check if there is any resumption token String resumptionToken = listRecords.getResumptionToken(); if (resumptionToken == null || resumptionToken.length() == 0) { listRecords = null; } else { log.info("Got resumption token"); listRecords = new ListRecords(target, resumptionToken); } } if (exService != null) { while (exService.getQueue().size() > 0) { log.info("Runnables left in queue: " + exService.getQueue().size() + ", waiting"); Thread.sleep(2000); } exService.shutdown(); } log.info("OAI-PMH harvester done with execution"); long diff = new Date().getTime() - before.getTime(); if (j > 0) { log.info("Harvesting of " + j + " records took " + diff + " ms (average of " + diff / j + " ms per record)"); } }
From source file:org.geppetto.frontend.messaging.DefaultMessageSender.java
private void submitTask(ThreadPoolExecutor executor, Runnable task) throws InterruptedException { if (discardMessagesIfQueueFull) { executor.execute(task); } else {/*ww w .ja va2s . co m*/ executor.getQueue().put(task); } }