如何将值返回到接收器流| Spark流媒体

时间:2017-07-19 10:51:53

标签: java apache-spark queue jms spark-streaming

我有以下课程:

public class QueueProcessor implements Runnable {
    //private static final Logger logger = LogManager.getLogger(QueueProcessor.class.getCanonicalName());
    private Session session;
    private Queue queue;
    private String queueName;
    private JSONObject jaob;


    public QueueProcessor(Session session, Queue queue, String queueName, JSONObject jaob) {
        this.queue = queue;
        this.session = session;
        this.queueName = queueName;
        this.jaob = jaob;
    }

    @Override
    public void run() {
        try {
            customConsumer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public synchronized void customConsumer() throws Exception {
        MessageConsumer consumer = session.createConsumer(queue);
        try {
            while (true) {
                Message msg = consumer.receive();
                if (msg != null && msg instanceof TextMessage) {
                    TextMessage tm = (TextMessage) msg;
                    JobProcessing jobProcessing = new JobProcessing(tm, queueName, jaob);
                    String finalJson = jobProcessing.process();
                    // System.out.println("Json copied for posting: " + finalJson);
                    GlobalAsyncHttpClient.getINSTANCE().postData(finalJson);
                    msg.acknowledge();
                }
            }
        } catch (Exception ex) {
           ex.printStackTrace();
        } finally {
            consumer.close();
        }
    }

}

现在我有大约8个队列,为此我产生8个线程来读取每个队列并使用Executors / ThreadPool处理上面的类。

我正在使用Spark流自定义来从JMS队列中读取。根据官方示例,使用的代码如下:

public static void main(String[] args) {

        try {
            SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver");
            JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, new Duration(1000));


            // Create an input stream with the custom receiver on target ip:port and count the
            // words in input stream of \n delimited text (eg. generated by 'nc')
            JavaReceiverInputDStream<String> lines = ssc.receiverStream(new TibcoReceiver());
            JavaDStream<String> words = lines.flatMap(x -> Arrays.asList(SPACE.split(x)).iterator());
            JavaPairDStream<String, Integer> wordCounts = words.mapToPair(s -> new Tuple2<>(s, 1))
                    .reduceByKey((i1, i2) -> i1 + i2);

            wordCounts.print();
            ssc.start();
            ssc.awaitTermination();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里的兴趣点是: JavaReceiverInputDStream<String> lines = ssc.receiverStream(new TibcoReceiver());

我想在这里读取QueueProcessor中读取的值,但是我如何在连续的while循环中执行此操作。我不知道我可以在这里使用任何其他设计模式。请指导!

0 个答案:

没有答案