Java Design Model Application-Filter mode

Source: Internet
Author: User

The Storm engine calculates a batch of intermediate alarm results, sends a Kafka message to the alarm inbound service, the alarm inbound service receives the Kafka message, reads the intermediate alarm file, and after a series of processing, the final alarm is stored in MySQL.

In fact, the intermediate alarm results may have duplicate alarm, error alarm, useless alarm, alarm storage service will filter, compress the intermediate alarm, the user concerned about the alarm into the database. There are many steps to filtering, and the alarms that the customer cares about may change at any time, and the dead alarm filter will soon be unable to satisfy the application scenario, and the filter mode in this scenario is good enough to satisfy the uncertainty of business scalability.

Alarm inbound service involves message filtering and alarm filtering, so let's talk about filter mode with a message filter.

1. Message Filter Interface

 PackageCom.coshaho.learn.filter;/*** * Imessagefilter.java Create on May 13, 2017 12:43:56 * class function Description: Alarm Message Filter * * Copyright:copyright (c) 201 3 * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public Abstract classIMessageFilterImplementsComparable<imessagefilter>{     Public intPriority () {return0; }         Public Abstract BooleanDoFilter (Message msg);  Public intcompareTo (IMessageFilter arg0) {returnPriority ()-arg0.priority (); }}

2. Time Filter

 PackageCom.coshaho.learn.filter;Importorg.springframework.stereotype.Component;/*** * Timefilter.java Create on May 13, 2017 12:44:25 * class function Description: Time filter * * Copyright:copyright (c) * C Ompany:coshaho * @Version 1.0 * @Author Coshaho*/@Component Public classTimefilterextendsimessagefilter{ Public intPriority () {return1; }     Public BooleanDoFilter (Message msg) {if(Msg.gethour () < 8 | | Msg.gethour () > 17) {System.out.println ("Time filter false, message is" +msg); return false; } System.out.println ("Time filter true, message is" +msg); return true; }}

3, Level Filter

 PackageCom.coshaho.learn.filter;Importorg.springframework.stereotype.Component;/*** * Serverityfilter.java Create on May 13, 2017 12:44:13 * class function Description: Level filter * * Copyright:copyright (c) 2013 * Company:coshaho * @Version 1.0 * @Author Coshaho*/@Component Public classServerityfilterextendsimessagefilter{ Public intPriority () {return2; }     Public BooleanDoFilter (Message msg) {if(msg.getseverity () = = 0) {System.out.println ("Severity filter false, message is" +msg); return false; } System.out.println ("Severity filter true, message is" +msg); return true; }}

4. Type Filter

 PackageCom.coshaho.learn.filter;Importorg.springframework.stereotype.Component;/*** * Typefilter.java Create on May 13, 2017 12:44:36 * class function Description: Type Filter * * Copyright:copyright (c) * Co Mpany:coshaho * @Version 1.0 * @Author Coshaho*/@Component Public classTypeFilterextendsimessagefilter{ Public intPriority () {return3; }     Public BooleanDoFilter (Message msg) {if(Msg.gettype () < 3) {System.out.println ("Type filter false, message is" +msg); return false; } System.out.println ("Type filter true, message is" +msg); return false; }        }

5. Message Monitoring Service

 PackageCom.coshaho.learn.filter;Importjava.util.Collections;Importjava.util.List;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importorg.springframework.stereotype.Component;/*** * Messagelistener.java Create on May 13, 2017 12:44:49 * class function Description: Alarm message Monitoring * * Copyright:copyright (c) 201 3 * Company:coshaho * @Version 1.0 * @Author Coshaho*/@Component Public classMessageListener { Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") ApplicationContext Context=NewClasspathxmlapplicationcontext ("Classpath:spring.xml"); MessageListener Listener= (MessageListener) context.getbean ("MessageListener"); Message msg=NewMessage (); Msg.sethour (12); Msg.setname ("Coshaho"); Msg.settype (5); Msg.setseverity (3);    Listener.listen (msg); }         Public voidListen (Message msg) {Excute (msg); }        Private BooleanExcute (Message msg) {@SuppressWarnings ("Unchecked") List<IMessageFilter> filters = (list<imessagefilter>) springutils.getbeansoftype (IMessageFilter.class);        Collections.sort (filters);  for(IMessageFilter filter:filters) {if(!Filter.dofilter (msg)) {                return false; }        }        return true; }}

6. Spring Tool class

 PackageCom.coshaho.learn.filter;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;Importorg.springframework.beans.BeansException;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.ApplicationContextAware;ImportOrg.springframework.stereotype.Service; @Service Public classSpringutilsImplementsapplicationcontextaware{Private StaticApplicationContext context;  Public voidSetapplicationcontext (applicationcontext context)throwsbeansexception {springutils.context=context; } @SuppressWarnings ({"Rawtypes", "unchecked" })     Public StaticList Getbeansoftype (Class clazz) {map map=Context.getbeansoftype (Clazz); return NewArrayList (Map.values ()); }}

7. Message Class

 PackageCom.coshaho.learn.filter;/*** * Message.java Create on May 13, 2017 12:43:37 * class function Description: Alarm message * * Copyright:copyright (c) * Compa Ny:coshaho * @Version 1.0 * @Author Coshaho*/ Public classMessage {PrivateString name; Private inthour; Private inttype; Private intseverity;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGethour () {returnhour; }     Public voidSethour (inthour) {         This. Hour =hour; }     Public intGetType () {returntype; }     Public voidSetType (inttype) {         This. Type =type; }     Public intgetseverity () {returnseverity; }     Public voidSetseverity (intseverity) {         This. Severity =severity; } @Override PublicString toString () {return"Message [name=" + name + ", hour=" + Hour + ", type=" +type+ ", severity=" + severity + "]"; }}

As you can see, filter mode can easily expand the filtering business.

Java Design Model Application-Filter mode

Related Article

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.