您的位置:首页 > 其它

基于Pipe的PureMVC Flex框架的多核共享消息技术

2012-11-09 09:50 387 查看
PureMVC在某一个版本更新后提供了管道技术,但是这项技术如何使用往往让你摸不着头脑。下面带着大家将它的使用方法展现给大家。

管道的一个重要特征就是你可以利用一个已经开发完成的工程,仅仅针对每一个需要集成在一起的模块定义一个JunctionMediator。

让我们看一个简单的例子,假设一个应用系统需要加载一个模块,同时需要跟它进行通讯。同时我们假定你想通过这种设计的方式跟众多其他的模块进行通讯。那么你需要在项目中为项目应用程序新建一个JunctionMediator类,同时还需要给模块新建一个JuntionMediator类。下面我们从一定高度上进行讲解一下mediators的实现细节。

下面是一个管道结构图:



我们的JuntionMediator是通过许多管道连接在一起。我们需要一个从app到module的消息流管道,还需要一个从module到app的消息流管道。

在管道的接收处还需要定义消息的监听者,但是同一个管道,我们只需要定义一个管道监听者。我们需要利用管道的监听者去处理即将到来的管道消息。

利用管道线去发送消息,我们一般需要利用Junction的SendMessage函数。

l

现在回到我们的简单例子上来,我们的应用的JunctionMediator是负责建立app的管道配件,创建去连接这些module的管道。

所以我们app的JunctionMediator需要做的第一步就是在项目启动时创建一个新的Junction。

public class ApplicationJunctionMediator extends JunctionMediator

{

public function ApplicationJunctionMediator()

{

super(NAME, new Junction());

}

一旦Mediator在façade模型中被注册,我们下一步需要做的就是去创建管道配件。创建这些配件,我们需要用到TeeSplit去处理分发(app到module)的消息,Teemerge去合并(module到app)消息。TeeSplit允许我们终端去连接很多module,而TeeMerge允许我们合并很多module到app应用程序。同时我们还需要为TeeMerge添加一个管道的监听者。这个监听者负责去处理所有的通过TeeMerge连接到app的模块发送过来的消息。

1 override public function onRegister():void

2

3 {

4

5 junction.registerPipe( PipeAwareModule.APP_TO_MODULE_PIPE, Junction.OUTPUT, new TeeSplit() );

6

7 junction.registerPipe( PipeAwareModule.MODULE_TO_APP_PIPE, Junction.INPUT, new TeeMerge() );

8

9 junction.addPipeListener( PipeAwareModule.MODULE_TO_APP_PIPE, this, handlePipeMessage );

10

11 }

handlePipeMessage函数是module到app的管道消息处理函数。

1 function handlePipeMessage(message:IPipeMessage):void

作为一个Mediator,我们可以去定义app级别感兴趣的消息,同时提供处理这些消息的方法。也就是说我们需要用listNotificationInterests()和handleNotification(note:INotification)来完成上面的功能。我们利用这些去监听消息,然后将消息通过管道传递下去。

举例:我们的Mortgage应用程序将要监听loan notification请求,然后将消息通过messages转发下去。

1 override public function handleNotification(note:INotification):void

2

3 {

4

5

6

7 switch( note.getName() )

8

9 {

10

11 case MortgageAppEventNames.REQUEST_FOR_LOAN:

12

13 var loanMessage:Message = new Message(MortgageAppEventNames.REQUEST_FOR_LOAN,null,note);

14

15 junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE,loanMessage);

16

17 break;

18

19 }

20

21 }

我们应用程序JunctionMediator将监听modules的loan notification消息,然后应用程序的JunctionMediator会创建一些管道连接应用程序的模块中得JunctionMediator,然后分发出去。

1 override public function handleNotification(note:INotification):void

2

3 {

4

5

6

7 switch( note.getName() )

8

9 {

10

11 case MortgageAppEventNames.REQUEST_FOR_LOAN:

12

13 var loanMessage:Message = new Message(MortgageAppEventNames.REQUEST_FOR_LOAN,null,note);

14

15 junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE,loanMessage);

16

17 break;

18

19

20

21 case ModuleEvents.CONNECT_MODULE_JUNCTION:

22

23 var module:IPipeAwareModule = note.getBody() as IPipeAwareModule;

24

25

26

27 // Create the pipe

28

29 var moduleToApp:Pipe = new Pipe();

30

31 // Connect the pipe to our module

32

33 module.acceptOutputPipe(PipeAwareModule.MODULE_TO_APP_PIPE, moduleToApp);

34

35

36

37 // Connect the pipe to our app

38

39 var appIn:TeeMerge = junction.retrievePipe(PipeAwareModule.MODULE_TO_APP_PIPE) as TeeMerge;

40

41 appIn.connectInput(moduleToApp);

42

43

44

45 // Create the pipe

46

47 var appToModule:Pipe = new Pipe();

48

49 // Connect the pip to our module

50

51 module.acceptInputPipe(PipeAwareModule.APP_TO_MODULE_PIPE,appToModule);

52

53

54

55 // Connect the pipe to our app

56

57 var appOut:TeeSplit = junction.retrievePipe(PipeAwareModule.APP_TO_MODULE_PIPE) as TeeSplit;

58

59 appOut.connect(appToModule);

60

61

62

63 break;

64

65

66

67 // And let super handle the rest (ACCEPT_OUTPUT_PIPE, ACCEPT_INPUT_PIPE, SEND_TO_LOG)

68

69 default:

70

71 super.handleNotification(note);

72

73

74

75 }

76

77 }

最后我们的应用程序的JunctionMediator会处理这些到来的管道消息。

1 override public function handlePipeMessage(message:IPipeMessage):void

2

3 {

4

5 // Handle our Module->Application integration

6

7 trace(message);

8

9 var note:INotification = message.getBody() as INotification;

10

11

12

13 switch(note.getName())

14

15 {

16

17 case MortgageAppEventNames.LOAN_QUOTE_READY:

18

19 sendNotification(note.getName(),note.getBody(),note.getType());

20

21 break;

22

23 default:

24

25 sendNotification(note.getName(),note.getBody(),note.getType());

26

27 break;

28

29 }

30

31 }

32

33

34

35

36

37 That covers the basic responsibilities for our app’s JunctionMediator.

38

39 上面包括了基本的应用程序JunctionMediator的响应过程。

40

41

42

43

44

45 Our module’s JunctionMediator would listen for messages coming in on the APP_TO_MODULE named pipe and send [outbound] messages on the MODULE_TO_APP pipe.

46

47

48

49

50

51 override public function handleNotification(note:INotification):void

52

53 {

54

55 // Handle our Module->Application integration

56

57 switch( note.getName() )

58

59 {

60

61 case ModuleFacade.QUOTE_GENERATED:

62

63 // convert our *local* notification into the application format

64

65 var quoteMessage:Message = new Message(MortgageAppEventNames.LOAN_QUOTE_READY,null,

66

67 new Notification(MortgageAppEventNames.LOAN_QUOTE_READY,note.getBody(),note.getType()));

68

69 junction.sendMessage(PipeAwareModule.MODULE_TO_APP_PIPE,quoteMessage);

70

71 break;

72

73 }

74

75 }

76

77

78

79 override public function handlePipeMessage( message:IPipeMessage ):void

80

81 {

82

83 // Handle our Application->Module integration

84

85 var note:INotification = message.getBody() as INotification;

86

87 switch(note.getName())

88

89 {

90

91 case MortgageAppEventNames.REQUEST_FOR_LOAN:

92

93 sendNotification(ModuleFacade.QUOTE_REQUESTED,note.getBody(),note.getType());

94

95 break;

96

97 }

98

99

100

101 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: