Storm1.2.1-receive ACTIVEMQ count the number of words, the results sent to ACTIVEMQ_ACTIVEMQ

Source: Internet
Author: User

SOURCE Download:

https://download.csdn.net/download/adam_zs/10338151


* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license.  The NOTICE file * Distributed with this work for additional information * regarding copyright. The ASF licenses this file * to you under the Apache License, Version 2.0 (The * "License");  You are not to use this file, except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicab Le or agreed to in writing, software * Distributed under the License was distributed on ' as is ' basis, * without W
 Arranties or CONDITIONS of any KIND, either express OR implied.
 * The License for the specific language governing permissions and * limitations under the License.

* * Package COM.WANGZS.SPRINGJMS;

Import javax.jms.Session;
Import Org.apache.storm.Config;
Import Org.apache.storm.LocalCluster;
Import Org.apache.storm.StormSubmitter; Import Org.apache.storm.jms.JmSprovider;
Import Org.apache.storm.jms.JmsTupleProducer;
Import Org.apache.storm.jms.spout.JmsSpout;
Import Org.apache.storm.topology.TopologyBuilder;

Import Org.apache.storm.utils.Utils; public class Examplejmstopology {public static void main (string[] args) throws Exception {//JMS Queue Provider J Msprovider Jmsqueueprovider = new Springjmsprovider ("Spring/applicationcontext-activemq.xml", "jmsConnectionFactory

		"," Notificationqueue ");

		JMS Producer jmstupleproducer Producer = new Jsontupleproducer ();
		JMS Queue spout Jmsspout queuespout = new Jmsspout ();
		Queuespout.setjmsprovider (Jmsqueueprovider);
		Queuespout.setjmstupleproducer (producer);
		Queuespout.setjmsacknowledgemode (Session.client_acknowledge); Queuespout.setdistributed (TRUE);

		Allow multiple instances Topologybuilder builder = new Topologybuilder ();
		Builder.setspout ("Jms-spout", Queuespout, 5);
		Builder.setbolt ("Split-bolt", New Splitbolt (), ten). Shufflegrouping ("Jms-spout"); BuildEr.setbolt ("Count-bolt", New Countbolt (), 1). globalgrouping ("Split-bolt");
		Config conf = new config ();

			if (Args.length > 0) {conf.setnumworkers (3);
		Stormsubmitter.submittopology (Args[0], conf, builder.createtopology ());
			else {Conf.setdebug (true);
			Localcluster cluster = new Localcluster ();
			Cluster.submittopology ("Storm-jms-example", conf, Builder.createtopology ());
			Utils.sleep (100000);
			Cluster.killtopology ("Storm-jms-example");
		Cluster.shutdown ();
 }
	}

}
Package COM.WANGZS.SPRINGJMS;
Import java.util.List;

Import Java.util.Map;
Import Org.apache.storm.task.OutputCollector;
Import Org.apache.storm.task.TopologyContext;
Import Org.apache.storm.topology.OutputFieldsDeclarer;
Import Org.apache.storm.topology.base.BaseRichBolt;
Import Org.apache.storm.tuple.Fields;
Import Org.apache.storm.tuple.Tuple;

Import org.apache.storm.tuple.Values;

	/** * For data segmentation/public class Splitbolt extends Baserichbolt {private Outputcollector collector; @Override public void Prepare (Map stormconf, Topologycontext context, Outputcollector collector) {this.collector = col
	Lector;
		@Override public void Execute (Tuple input) {List values = input.getvalues ();
		String line = (string) values.get (0);
		string[] Words = Line.split ("");
		for (String word:words) {this.collector.emit (new Values (word));
	} this.collector.ack (input); @Override public void Declareoutputfields (Outputfieldsdeclarer declarer) {declarer.declare (new Fields("word"));
 }

}

Package COM.WANGZS.SPRINGJMS;
Import Java.util.HashMap;
Import Java.util.Map;

Import Java.util.Set;
Import javax.jms.JMSException;
Import Javax.jms.Message;

Import javax.jms.Session;
Import Org.apache.storm.task.OutputCollector;
Import Org.apache.storm.task.TopologyContext;
Import Org.apache.storm.topology.OutputFieldsDeclarer;
Import Org.apache.storm.topology.base.BaseRichBolt;
Import Org.apache.storm.tuple.Tuple;
Import Org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.jms.core.JmsTemplate;

Import Org.springframework.jms.core.MessageCreator;

Import Net.sf.json.JSONObject; /** * Word Frequency Summary Bolt */public class Countbolt extends Baserichbolt {private hashmap<string, integer> map = new Hashma
	P<string, integer> ();

	Private Jmstemplate jmstemplate; @Override public void Prepare (Map stormconf, Topologycontext context, Outputcollector collector) {ApplicationContext A Pplicationcontext = new Classpathxmlapplicationcontext (new string[] {"Classpath*:spring/applicationcontext-*.xml"});
	This.jmstemplate = (jmstemplate) applicationcontext.getbean ("Jmstemplate");
		@Override public void Execute (Tuple input) {String word = Input.getstringbyfield ("word");
		Integer count = Map.get (word);
		if (count = = null) {count = 0;
		} count++;

		Map.put (Word, count);
		System.out.println ("~~~~~~~~~~~~execute~~~~~~~~~~");
		set<map.entry<string, integer>> entryset = Map.entryset ();
		For (map.entry<string, integer> entry:entryset) {System.out.println (Entry);  } jmstemplate.send ("Queue_itshow", new Messagecreator () {@Override public message CreateMessage
			Throws JMSException {return session.createtextmessage (Jsonobject.fromobject (map). ToString ());
	}
		});
		@Override public void Declareoutputfields (Outputfieldsdeclarer declarer) {} @Override public void Cleanup () { System.out.println ("~ ~~~~~~~~~~~cleanup~~~~~~~~~~ ");
		set<map.entry<string, integer>> entryset = Map.entryset ();
		For (map.entry<string, integer> entry:entryset) {System.out.println (Entry);
 }
	}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.