Skip to content

Instantly share code, notes, and snippets.

@evasuarez22
Created April 22, 2016 10:12
Show Gist options
  • Save evasuarez22/c46c09d38dc2c2658c463fe92c7d39d8 to your computer and use it in GitHub Desktop.
Save evasuarez22/c46c09d38dc2c2658c463fe92c7d39d8 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2014 sinfonier-project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.sinfonier.spouts;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import backtype.storm.utils.Utils;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.security.SecureRandom;
import java.math.BigInteger;
public class Pulse extends BaseSinfonierSpout {
private LinkedBlockingQueue<String> queue = null;
private int frequency = 300;
private SecureRandom random = new SecureRandom();
public Pulse(String spoutName, String xmlPath) {
super(spoutName, xmlPath);
}
public void useropen(){
try {
frequency = Integer.parseInt(getParam("frequency",true));
} catch (Exception e) {
e.printStackTrace();
}
queue = new LinkedBlockingQueue<String>(1000);
JobDetail job = JobBuilder.newJob(PulseJob.class)
.withIdentity("pulseJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("Generate Items")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(frequency)
.repeatForever()).build();
String schedulerName = new BigInteger(130, random).toString(32);
System.setProperty("org.quartz.scheduler.instanceName", schedulerName);
Scheduler scheduler;
try {
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
stdSchedulerFactory.initialize();
scheduler = stdSchedulerFactory.getScheduler();
scheduler.getContext().put("queue", queue);
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void usernextTuple(){
if (!queue.isEmpty()) {
String json = queue.poll();
this.emit();
} else {
Utils.sleep(50);
}
}
public void userclose() {
}
public static class PulseJob implements Job {
private JSONArray jsonLastArray = null;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SchedulerContext schedulerContext = null;
List<String> keys;
List<String> values;
int delay;
try {
schedulerContext = context.getScheduler().getContext();
@SuppressWarnings("unchecked")
LinkedBlockingQueue<String> queue = (LinkedBlockingQueue<String>) schedulerContext
.get("queue");
JSONObject jobj = new JSONObject();
jobj.put(".",".");
queue.put(jobj.toString());
} catch (SchedulerException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment