source: java/main/src/main/java/com/framsticks/communication/Subscription.java @ 85

Last change on this file since 85 was 85, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 2.1 KB
Line 
1package com.framsticks.communication;
2
3import java.util.List;
4
5import com.framsticks.communication.queries.RegistrationRequest;
6import com.framsticks.util.dispatching.Dispatcher;
7import com.framsticks.util.dispatching.Dispatching;
8import com.framsticks.util.dispatching.RunAt;
9import com.framsticks.util.StateFunctor;
10import org.apache.log4j.Logger;
11
12
13/**
14 * @author Piotr Sniegowski
15 */
16public class Subscription<C> {
17
18        private final static Logger log = Logger.getLogger(Subscription.class);
19
20        private final ClientConnection connection;
21        private final String path;
22        private final String registeredPath;
23        private final Dispatcher<C> dispatcher;
24
25        private EventCallback eventCallback;
26
27        public Subscription(ClientConnection connection, String path, String registeredPath, Dispatcher<C> dispatcher) {
28                this.connection = connection;
29                this.path = path;
30                this.registeredPath = registeredPath;
31                this.dispatcher = dispatcher;
32        }
33
34        public String getPath () {
35                return path;
36        }
37
38        public String getRegisteredPath() {
39                return registeredPath;
40        }
41
42        @Override
43        public String toString() {
44                return path + "(" + registeredPath + ")";
45        }
46
47        public void unsubscribe(final StateFunctor stateFunctor) {
48                //@todo remove that /cli/ prefix, when registeredPath will be a fully qualified path
49                connection.send(new RegistrationRequest().register(false).path(registeredPath), new ResponseCallback<Connection>() {
50                        @Override
51                        public void process(Response response) {
52                                if (!response.getOk()) {
53                                        log.error("failed to unsunscribe " + this + ": " + response.getComment());
54                                        stateFunctor.call(new Exception(response.getComment()));
55                                        return;
56                                }
57                                assert response.hasFiles();
58                                log.debug("unsunscribed " + this);
59                                stateFunctor.call(null);
60                        }
61                });
62        }
63
64        public EventCallback getEventCallback() {
65                return eventCallback;
66        }
67
68        public Dispatcher<C> getDispatcher() {
69                return dispatcher;
70        }
71
72        public void setEventCallback(EventCallback eventCallback) {
73                this.eventCallback = eventCallback;
74        }
75
76        public void dispatchCall(final List<File> files) {
77                Dispatching.invokeLaterOrNow(dispatcher, new RunAt<C>() {
78                        @Override
79                        public void run() {
80                                eventCallback.call(files);
81                        }
82                });
83        }
84}
Note: See TracBrowser for help on using the repository browser.