com.autsia.socialboot.businesslogic.twitter.callables.TwitterCallable.java Source code

Java tutorial

Introduction

Here is the source code for com.autsia.socialboot.businesslogic.twitter.callables.TwitterCallable.java

Source

/*
 *    Copyright 2015 Dmytro Titov
 *
 *    Licensed 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 com.autsia.socialboot.businesslogic.twitter.callables;

import com.autsia.socialboot.SocialBootApplication;
import com.autsia.socialboot.businesslogic.nlp.model.Sentiment;
import com.autsia.socialboot.businesslogic.nlp.processors.NLPProcessor;
import com.autsia.socialboot.businesslogic.twitter.dto.TweetWrapper;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.core.IQueue;
import com.hazelcast.spring.context.SpringAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.concurrent.Callable;

/**
 * Callable for Hazelcast to process tweet on a remote node
 */
@SpringAware
public class TwitterCallable implements Callable<TweetWrapper>, Serializable, HazelcastInstanceAware {

    private static final Logger LOGGER = LoggerFactory.getLogger(TwitterCallable.class);

    private transient HazelcastInstance hazelcastInstance;

    @Autowired
    private transient NLPProcessor nlpProcessor;

    private TweetWrapper tweetWrapper;

    public TwitterCallable(TweetWrapper tweetWrapper) {
        this.tweetWrapper = tweetWrapper;
    }

    @Override
    public TweetWrapper call() throws Exception {
        Sentiment sentiment = nlpProcessor.findSentiment(tweetWrapper.getText());
        tweetWrapper.setSentiment(sentiment);
        IQueue<Object> queue = hazelcastInstance.getQueue(SocialBootApplication.class.getSimpleName());
        queue.put(tweetWrapper);
        return tweetWrapper;
    }

    @Override
    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
        this.hazelcastInstance = hazelcastInstance;
    }

}