Search in sources :

Example 1 with JmsProvider

use of org.apache.storm.jms.JmsProvider in project storm by apache.

the class ExampleJmsTopology method main.

@SuppressWarnings("serial")
public static void main(String[] args) throws Exception {
    // JMS Queue Provider
    JmsProvider jmsQueueProvider = new SpringJmsProvider("jms-activemq.xml", "jmsConnectionFactory", "notificationQueue");
    // JMS Topic provider
    JmsProvider jmsTopicProvider = new SpringJmsProvider("jms-activemq.xml", "jmsConnectionFactory", "notificationTopic");
    // JMS Producer
    JmsTupleProducer producer = new JsonTupleProducer();
    // JMS Queue Spout
    JmsSpout queueSpout = new JmsSpout();
    queueSpout.setJmsProvider(jmsQueueProvider);
    queueSpout.setJmsTupleProducer(producer);
    queueSpout.setJmsAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    // allow multiple instances
    queueSpout.setDistributed(true);
    TopologyBuilder builder = new TopologyBuilder();
    // spout with 5 parallel instances
    builder.setSpout(JMS_QUEUE_SPOUT, queueSpout, 5);
    // intermediate bolt, subscribes to jms spout, anchors on tuples, and auto-acks
    builder.setBolt(INTERMEDIATE_BOLT, new GenericBolt("INTERMEDIATE_BOLT", true, true, new Fields("json")), 3).shuffleGrouping(JMS_QUEUE_SPOUT);
    // bolt that subscribes to the intermediate bolt, and auto-acks
    // messages.
    builder.setBolt(FINAL_BOLT, new GenericBolt("FINAL_BOLT", true, true), 3).shuffleGrouping(INTERMEDIATE_BOLT);
    // bolt that subscribes to the intermediate bolt, and publishes to a JMS Topic
    JmsBolt jmsBolt = new JmsBolt();
    jmsBolt.setJmsProvider(jmsTopicProvider);
    // anonymous message producer just calls toString() on the tuple to create a jms message
    jmsBolt.setJmsMessageProducer(new JmsMessageProducer() {

        @Override
        public Message toMessage(Session session, ITuple input) throws JMSException {
            System.out.println("Sending JMS Message:" + input.toString());
            TextMessage tm = session.createTextMessage(input.toString());
            return tm;
        }
    });
    builder.setBolt(JMS_TOPIC_BOLT, jmsBolt).shuffleGrouping(INTERMEDIATE_BOLT);
    // JMS Topic spout
    JmsSpout topicSpout = new JmsSpout();
    topicSpout.setJmsProvider(jmsTopicProvider);
    topicSpout.setJmsTupleProducer(producer);
    topicSpout.setJmsAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    topicSpout.setDistributed(false);
    builder.setSpout(JMS_TOPIC_SPOUT, topicSpout);
    builder.setBolt(ANOTHER_BOLT, new GenericBolt("ANOTHER_BOLT", true, true), 1).shuffleGrouping(JMS_TOPIC_SPOUT);
    Config conf = new Config();
    if (args.length > 0) {
        conf.setNumWorkers(3);
        StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
    } else {
        conf.setDebug(true);
        try (LocalCluster cluster = new LocalCluster();
            LocalTopology topo = cluster.submitTopology("storm-jms-example", conf, builder.createTopology())) {
            Utils.sleep(60000);
        }
    }
}
Also used : JmsSpout(org.apache.storm.jms.spout.JmsSpout) JmsMessageProducer(org.apache.storm.jms.JmsMessageProducer) LocalCluster(org.apache.storm.LocalCluster) JmsBolt(org.apache.storm.jms.bolt.JmsBolt) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) Config(org.apache.storm.Config) JMSException(javax.jms.JMSException) LocalTopology(org.apache.storm.LocalCluster.LocalTopology) Fields(org.apache.storm.tuple.Fields) ITuple(org.apache.storm.tuple.ITuple) JmsProvider(org.apache.storm.jms.JmsProvider) JmsTupleProducer(org.apache.storm.jms.JmsTupleProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 2 with JmsProvider

use of org.apache.storm.jms.JmsProvider in project storm by apache.

the class JmsSpoutTest method testFailure.

@Test
public void testFailure() throws JMSException, Exception {
    JmsSpout spout = new JmsSpout();
    JmsProvider mockProvider = new MockJmsProvider();
    MockSpoutOutputCollector mockCollector = new MockSpoutOutputCollector();
    SpoutOutputCollector collector = new SpoutOutputCollector(mockCollector);
    spout.setJmsProvider(new MockJmsProvider());
    spout.setJmsTupleProducer(new MockTupleProducer());
    spout.setJmsAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    // Rapid recovery for testing.
    spout.setRecoveryPeriod(10);
    spout.open(new HashMap<String, String>(), null, collector);
    Message msg = this.sendMessage(mockProvider.connectionFactory(), mockProvider.destination());
    Thread.sleep(100);
    // Pretend to be storm.
    spout.nextTuple();
    Assert.assertTrue(mockCollector.emitted);
    mockCollector.reset();
    // Mock failure
    spout.fail(msg.getJMSMessageID());
    Thread.sleep(5000);
    // Pretend to be storm.
    spout.nextTuple();
    Thread.sleep(5000);
    // Should have been re-emitted
    Assert.assertTrue(mockCollector.emitted);
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) JmsProvider(org.apache.storm.jms.JmsProvider) Test(org.junit.Test)

Aggregations

Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 JmsProvider (org.apache.storm.jms.JmsProvider)2 JMSException (javax.jms.JMSException)1 Session (javax.jms.Session)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 JmsMessageProducer (org.apache.storm.jms.JmsMessageProducer)1 JmsTupleProducer (org.apache.storm.jms.JmsTupleProducer)1 JmsBolt (org.apache.storm.jms.bolt.JmsBolt)1 JmsSpout (org.apache.storm.jms.spout.JmsSpout)1 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)1 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)1 Fields (org.apache.storm.tuple.Fields)1 ITuple (org.apache.storm.tuple.ITuple)1 Test (org.junit.Test)1