Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the lightweight Workflow Framework of Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor shares with you the basic introduction of Spring's lightweight workflow framework. I believe most people don't know much about it. In order to make you understand better, the editor summed up the following content for you. Let's move on.

First, a popular introduction to the framework.

The framework is inspired by the real public transport system. The most important elements of the public transport system and their correspondence to the workflow framework:

Passenger: data in the corresponding workflow framework (data)

Bus: the Carrier of data

Station: a station can be regarded as a node in the workflow, which is responsible for dealing with the "passengers" on the "bus".

Lines: which nodes make up a complete workflow processing chain

Does it feel that the whole bus system is a huge workflow processing network, with buses departing from the station, arriving at one station all the time, and passengers going up and down to the next station (of course, as long as there is no accident (exception)).

Some important interfaces BusContext in the framework

Save a context for business processing logic.

Bus

A Bus is the context in which a business process is saved, the starting node of the business, what to do when an exception is thrown, and so on. A new bus is created for each business flow to be processed along a node.

Station

Station is a single node in a business flow (processing chain). This node should only rely on the context in Bus, process it according to the context of bus, and put the processed results (if any) into the context of bus for use by downstream nodes. For example, here is a Station that gets maxValue and minValue from the Bus context. If the difference between them is less than 10, the key of the route is set to OK (Routing routes based on this)

Routing

Since there is no direct connection between Station, Routing is responsible for connecting each Station, and each Station has a Routing to deal with which Station of the bus, that is, it can dynamically determine the next Station of the Bus.

How to use the example Station

A Station is a Spring container-managed Bean (which implements the com.lizo.busflow.station.Station interface). A station should be an independent, general-purpose business processing class, such as a parameter checker, ip control, or a relatively opposite business logic, etc.

Public class GetDiff implements Station {public void abstractCalculate (@ BusParameter ("maxValue") int a, @ BusParameter ("minValue") int b, Bus bus) {if (Math.abs (a-b) < 10) {bus.setRoutingKey ("ok");} else {bus.setRoutingKey ("no");} @ Override public String getName () {return null;}}

Routing

The Routing must have a corresponding Station. For example, in the xml configuration, you can select the Station for the next processing according to the key of the route.

Be careful,

By default, Station refers to the Bean that contains Routing, not the Bean of Station.

Ref is required to develop a Spring bean, and that method is used to handle method formulation.

By default, the parameter name of the key corresponding method of BusContext is used for automatic injection. If there is a special need, you can use the @ BusParameter annotation to specify whether the key corresponding to BusContext is required (default is required, set to non-required, default value will be injected).

Bus

A complete Bus is defined in xml, as follows:

Where:

Id: the name of the corresponding Spring Bean

Start: start Routing for the corresponding workflow

MaxPath: specifies that bus will run out of exceptions if the number of times it is processed is greater than this number (to prevent an endless loop)

Exception: specifies which Station will handle the exception when it is sent, such as a Station that logs errors

Finish: indicates which Station will handle the last when the whole process is finished

Class: specify the type of bus and use the default com.lizo.busflow.bus.DefaultBus if it is empty

Look at a DEMO.

Now there is a business requirement that needs to be done as follows

Enter an integer list

Find out the maximum and minimum values

If the difference between the maximum and minimum values is greater than 10, output "no", otherwise enter "ok", of course, the business process in the real project will not be so simple, just use this as an example here

The first step is to write an independent Station

For example, the core code of getDiff is as follows:

Public class GetDiff implements Station {public void abstractCalculate (@ BusParameter ("maxValue") int a, @ BusParameter ("minValue") int b, BusContext busContext) {if (Math.abs (a-b) < 10) {busContext.setRoutingKey ("ok");} else {busContext.setRoutingKey ("no");} @ Override public String getName () {return null;}}

String them together.

Create a bus, and the driver runs demopublic class DemoApplication {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("classpath:bus-config.xml"); Bus testBus = BusFactory.createNewBus ("testBus"); List input = Arrays.asList (5,7,1,0,1,3,4,5,6,4); testBus.putContext ("intList", input); testBus.run () TestBus = BusFactory.createNewBus ("testBus"); input = Arrays.asList (52,7,1,-10,1,3,4,5,6,4); testBus.putContext ("intList", input); testBus.run ();}}

After reading the appeal, do you have a general understanding of Spring's lightweight workflow framework? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report