User / Application specific values in Twisted .tac file - twisted

What's the best practice for putting additional configuration items, specific to my application, in a twistd ".tac" file? How do I access these items from inside my class?
Is there some property in the "application" object that's intended to store these?

Create your own twisted.application.service.IService implementation (by subclassing twisted.application.service.Service or just by implementing the correct methods and attributes on a class all of your own). Give this class an __init__ that accepts the application-specific parameters. Launch the rest of your application logic in the startService method that is automatically called when twistd starts the reactor (for all IService objects attached to application). Use the objects you passed to __init__ in startService to get your application going in the right direction.
For example, see the FingerService defined in one of the Twisted tutorials (but unlike that tutorial, don't define all your classes in the .tac file! define them in modules and import them into the .tac file).

Related

How to create a custom NiFi Controller Service?

I am trying to learn, how to create a custom NiFi controller service. To start off, I thought of mimicking the DBCPConnectionPool controller service by simply copying the original source code of DBCPConnectionPool service. To implement the same, I generated a maven archetype from "nifi-service-bundle-archetype" and got the following project structure
However, when i generated the archetype from 'nifi-processor-bundle-archetype , I got the following structure: -
I understand that in case of processor I simply need to write my code in MyProceesor.java present under nifi-ListDbTableDemo-processors folder and then create a nar file out of it. But in case of controller service, I have 4 folders generated. I can see two java files i.e.
StandardMyService.java present under nifi-DbcpServiceDemo folder
MyService.java present under nifi-DbcpServiceDemo-apifolder
Now, why is there two java files generated in case of custom controller service, while there was only one java file generated in case of custom processor. Also, Since I am trying to mimick the DBCPConnectionPool service, in which java file out of two should I copy the original source code of DBCPConnectionPool service.
Please guide me from scratch, the steps that I need to follow to create a custom service equivalent to that of DBCPConnectionPool service.
MyService.java under nifi-DbcpServiceDemo-api is an interface which be implemented by the StandardMyService.java under nifi-DbcpServiceDemo. Once the implementation is done, you have to use nifi-DbcpServiceDemo-api as dependency in the processor bundle which needs to work with this custom controller Service.
The reason why controller services are implemented this way is:
We will be hiding the actual implementation from the processor bundle because it need not depend on the implementation.
Tomorrow you write a new controller service implementation, say StandardMyServiceTwo which again implements MyService because only the implementation varies from StandardMyService and other members remains the same and can be shared. This new controller service can be introduced transparently without making any changes on the processor bundle.
Example:
The best example is the record reader/writer controller services. If you look at the nifi-record-serialization-services-bundle in nifi, they have different implementation for serializing records of JSON, Grok, avro, CSV data formats but they all are actually implementing one API - nifi-record-serialization-service-api And hence for the processors which want to use the Record Reader or Record Writer, instead of having the actual implementations as its dependency, they rather can have the api as its dependency.
So tomorrow you can add add a new implementation in the record-serialization-services-bundle for a new data format without touching anything on the processors bundle.
For you references, please take a look at the following links which would help you in writing the custom controller service from scratch
http://www.nifi.rocks/developing-a-custom-apache-nifi-controller-service/
https://github.com/bbende/nifi-dependency-example

What is the use of service.method attribute in Moqui?

I am not able to figure out the method in which I can use the service.method attribute in Moqui. Is this similar to that of service.invoke attribute in OFBIz which allows me to call the method form the file specified in the location.
I was not able to find any examples related to this anywhere in the code.
The annotation in the XSD for the service.#method attribute says: "The method within the location, if applicable to the service type."
To expand on that, for Java classes this is the method within the class. For Groovy scripts you can call a method in the script (or Groovy class) instead of running the top-level of the script.

CDI environment configuration, handling properties with annotations

Found DeltaSpike that sort of does what I want....
Working with EJB 3.1 and really want to be able to annotate class properties so that "environment" properties are injected either at class load time or lazy on access plus the ability to allow for updates. For example:
#Environment(name="greeting.for.aliens", fetch="lazy", updates="true")
private String welcomeSign;
The name attribute is a match on a property name pulled from different sources like Java System, environment variables (from the shell), JNDI, property files and so forth. The fetch either says load on initiation or lazy (underpinned by Instance wrapper). Then if the "environment" properties are updated the interested "class" properties listen for that event.
More amazed that nobody has put together something (beyond DeltaSpikes) like this for EJB/CDI? Or I haven't found a good combination of Google keys words to get me to the right implementation for me.

How to I declare to Ocean that my custom domain object can free memory?

I'd like to link my custom domain object into the Petrel free memory command. My domain object caches data while visualised and this cache could be cleared when the user wants to free memory.
I have found the IMemorySaver interface and tried declaring this on my custom domain object but the FreeMemory method is not called when the user choose to free memory in Petrel.
Any ideas?
Neal
In Ocean 2013.1 a new API has been introduced that allows custom domain objects and ToggleWindows from a plug-in to be told when the user has invoked the ‘Free memory’ feature (this will also work for programmatic calls to PetrelSystem.ForceFreeMemory()).
The API follows a similar pattern to the existing INameInfoFactory and IImageInfoFactory APIs.
In order to use the API you need to create a factory object for your custom domain object (or ToggleWindow) that implements the new IResourceSaverFactory interface.
This interface requires that you implement a single method called GetResourceSaver(). This
method will return a ResourceSaver object that is associated with your custom domain object (or ToggleWindow).
ResourceSaver is an abstract class and you should implement the FreeResources() method on your derived class.
When the ‘Free memory’ feature is invoked the system will use your ResourceSaverFactory to obtain a ResourceSaver object for each of your custom domain object (or ToggleWindow) instances.
The FreeResources() method will be called on your ResouceSaver
objects.
Regards,
Chippy
Neal, the IMemorySaver is declared as a service interface, which you should not re-implement.
Having said that, participation in Petrel's controlled resource management is a fair requirement.

reading system.servicemodel section from database

We have a dynamically composed application, in which user can add services and operations. This application is installed on a server cluster.
Since adding services to application involves so much writing to web.config, i was wondering if its possible to read system.servicemodel section from a database instead of web.config.
Seems like microsoft's implementation of configuration is very tightly coupled with where its stored.
There is no "out-of-the-box" way to do that. However, it is possible.
Few feet below, Configuration class uses FileStream instance where it actually can use any Stream. That particular step can be replaced with custom implementation of IInternalConfigHost interface (a lot of properties and methods to implement there).
Particularly interesting are OpenStreamForRead and OpenStreamForWrite, both are returning Stream instances. There you can put logic to pull XML of configuration sections from database into ConfigurationSection instances and to put ConfigurationSection instances as XML into database.
The next step is to create an instance of Configuration class. However, here we must get dirty because its constructor never leaves the System.Configuration kingdom. The need to use reflection to reach and use it. I suggest implementation of IInternalConfigConfigurationFactory to wrap the reflection magic.
Configuration Create( Type typeConfigHost,
params object[] hostInitConfigurationParams );
As first parameter pass the type of implemented configuration host.
After we have Configuration instance, we can use it a custom ServiceHost, ChannelFactory<T> and DuplexChannelFactory<T>.