@@ -58,10 +58,13 @@ public void subscribe(String subject, MessageSubscriber subscriber) {
5858 assert (subject != null );
5959 assert (subscriber != null );
6060 if (_gate .enter ()) {
61- SubscriptionNode current = locate (subject , null , true );
62- assert (current != null );
63- current .addSubscriber (subscriber );
64- _gate .leave ();
61+ try {
62+ SubscriptionNode current = locate (subject , null , true );
63+ assert (current != null );
64+ current .addSubscriber (subscriber );
65+ } finally {
66+ _gate .leave ();
67+ }
6568 } else {
6669 synchronized (_pendingActions ) {
6770 _pendingActions .add (new ActionRecord (ActionType .Subscribe , subject , subscriber ));
@@ -72,14 +75,17 @@ public void subscribe(String subject, MessageSubscriber subscriber) {
7275 @ Override
7376 public void unsubscribe (String subject , MessageSubscriber subscriber ) {
7477 if (_gate .enter ()) {
75- if (subject != null ) {
76- SubscriptionNode current = locate (subject , null , false );
77- if (current != null )
78- current .removeSubscriber (subscriber , false );
79- } else {
80- _subscriberRoot .removeSubscriber (subscriber , true );
78+ try {
79+ if (subject != null ) {
80+ SubscriptionNode current = locate (subject , null , false );
81+ if (current != null )
82+ current .removeSubscriber (subscriber , false );
83+ } else {
84+ _subscriberRoot .removeSubscriber (subscriber , true );
85+ }
86+ } finally {
87+ _gate .leave ();
8188 }
82- _gate .leave ();
8389 } else {
8490 synchronized (_pendingActions ) {
8591 _pendingActions .add (new ActionRecord (ActionType .Unsubscribe , subject , subscriber ));
@@ -90,9 +96,12 @@ public void unsubscribe(String subject, MessageSubscriber subscriber) {
9096 @ Override
9197 public void clearAll () {
9298 if (_gate .enter ()) {
93- _subscriberRoot .clearAll ();
94- doPrune ();
95- _gate .leave ();
99+ try {
100+ _subscriberRoot .clearAll ();
101+ doPrune ();
102+ } finally {
103+ _gate .leave ();
104+ }
96105 } else {
97106 synchronized (_pendingActions ) {
98107 _pendingActions .add (new ActionRecord (ActionType .ClearAll , null , null ));
@@ -103,8 +112,11 @@ public void clearAll() {
103112 @ Override
104113 public void prune () {
105114 if (_gate .enter ()) {
106- doPrune ();
107- _gate .leave ();
115+ try {
116+ doPrune ();
117+ } finally {
118+ _gate .leave ();
119+ }
108120 } else {
109121 synchronized (_pendingActions ) {
110122 _pendingActions .add (new ActionRecord (ActionType .Prune , null , null ));
@@ -132,18 +144,19 @@ private void doPrune() {
132144 public void publish (String senderAddress , String subject , PublishScope scope , Object args ) {
133145
134146 if (_gate .enter (true )) {
147+ try {
148+ List <SubscriptionNode > chainFromTop = new ArrayList <SubscriptionNode >();
149+ SubscriptionNode current = locate (subject , chainFromTop , false );
135150
136- List <SubscriptionNode > chainFromTop = new ArrayList <SubscriptionNode >();
137- SubscriptionNode current = locate (subject , chainFromTop , false );
138-
139- if (current != null )
140- current .notifySubscribers (senderAddress , subject , args );
141-
142- Collections .reverse (chainFromTop );
143- for (SubscriptionNode node : chainFromTop )
144- node .notifySubscribers (senderAddress , subject , args );
151+ if (current != null )
152+ current .notifySubscribers (senderAddress , subject , args );
145153
146- _gate .leave ();
154+ Collections .reverse (chainFromTop );
155+ for (SubscriptionNode node : chainFromTop )
156+ node .notifySubscribers (senderAddress , subject , args );
157+ } finally {
158+ _gate .leave ();
159+ }
147160 }
148161 }
149162
0 commit comments