Featured post

Docker setup for Liferay 7 with MySQL

Tuesday, 10 April 2018

Osgi Target, Property attributes and Multiple Implementations

Osgi Target and Property attributes

In Spring you can use @Qualifier to get the correct class for respective instances.
How can we achieve this in OSGI framework is the question?

With help of Osgi and it's SCR(Service Component Runtime) you can use dynamic instantiation of Declarative Services.
Correct implementation should be selected for each instance to develop and use IS-A relationships.

Developers can instantiate a interface with different implementations by utlizing "target" attribute inside @Reference annotation. All you need is to enable the classes to get it from SCR with "property" attribute inside @Component annotation.

Below is the sample code to achieve the described matter

Problem Statement : We need to convert Numbers from string in different formats like Integer, Double, Float etc. and use them in different osgi components.


We will create interface and two classes which will implement this interface and consume them in another component for demonstration purpose.



public interface OsgiTarget {

 public Number convert(String str);
}

As you can see we defined multiple properties inside @Component annotation.

@Component(property = {
        "osgi.target.example.integer=true",
        "osgi.target.example.integer.again=true" // Dummy entry for multiple target example, you can get this instance with this property as well
})
public class OsgiTargetIntegerImpl implements OsgiTarget {
 @Override
 public Integer convert(String str) {
  return Integer.valueOf(str);
 }
}

Here we have single property for the same.

@Component(property = "osgi.target.example.double=true")
public class OsgiTargetDoubleImpl implements OsgiTarget{

 @Override
 public Double convert(String str) {
  return Double.valueOf(str);
 }
}


Now we have basic converters with different implementations.
We will use them in our component to show how it's going to work.


@Component(immediate = true)
public class OsgiTargetExampleConsumer {
 
 // Osgi target property for OsgiTargetIntegerImpl
 @Reference(target = "(osgi.target.example.integer=true)")
 OsgiTarget osgiTargetInteger;
 
 // Osgi target property for OsgiTargetIntegerImpl with another property but same instance 
 @Reference(target = "(osgi.target.example.integer.again=true)")
 OsgiTarget osgiTargetIntegerAgain;
 
 // Osgi target property for OsgiTargetDoubleImpl
 @Reference(target = "(osgi.target.example.double=true)")
 OsgiTarget osgiTargetDouble;

 
 @Activate
 public void run() {
  System.out.println("__________________" + osgiTargetInteger.convert("21"));
  System.out.println("__________________");
  System.out.println("__________________" + osgiTargetInteger.convert("12"));
  System.out.println("__________________");
  System.out.println("__________________" + osgiTargetDouble.convert("1987"));
 }
}


In the above class we are using the interface with @Reference annotation to get the object from SCR with respective classes just by adding target . If you look closely, you can see one class can be invoked by multiple properties as well.

Output will be similar like this :



You are just done, Try & Enjoy the function.............:)

29 comments:

  1. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. Sloane Residences City Condo

    ReplyDelete
    Replies
    1. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.


      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      Delete
  2. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  3. Useful post.All the details are explained in clear manner.Thanks for sharing this good information. Stock Audit | Customer Reconciliation | Inventory Audit

    ReplyDelete
  4. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. https://www.valsqld.com.au/

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Very helpful post. Very clear commentary and suggested phrasing are most impressive, and your generosity in sharing this explanation and example. Keep it up. Duplicate Payment Review
    Duplicate Payment Recovery
    Continuous Transaction Monitoring

    ReplyDelete
  7. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning Real Estate Developments Mount Sophia

    ReplyDelete
  8. This means that net and gross leases can be evident in the same property and have different impact on the outgoings position for the landlord.real estate practice test

    ReplyDelete
  9. Great Post,really it was very helpful for us.
    Thanks a lot for sharing!
    I found this blog to be very useful!!
    JAVA training in Bangalore

    ReplyDelete
  10. An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers. Repair your current gutters

    ReplyDelete
  11. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon. Choose the right Handyman

    ReplyDelete
  12. I wish more authors of this type of content would take the time you did to research and write so well. I am very impressed with your vision and insight. gutter repair raleigh

    ReplyDelete
  13. I am doing a report on this subject. Your article is full of really useful information. I will make sure to come back to check out your posts for my next report. Residential Gutter systems

    ReplyDelete
  14. Thanks for the valuable information and insights you have so provided here... Affinity at serangoon price

    ReplyDelete
  15. wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated. Seamless gutter company

    ReplyDelete
  16. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. 99 Years Leasehold Development

    ReplyDelete
  17. Understanding the type of rental charged across the property is essential to property performance.trulia ocala fl

    ReplyDelete
  18. This means that net and gross leases can be evident in the same property and have different impact on the outgoings position for the landlord.stop my foreclosure

    ReplyDelete
  19. I think I didn't write clearly. Where supply is inelastic, rents and supply are relatively unaffected by raising property taxes. Where supply is elastic, rents reflect marginal costs, so the tax falls on the consumer as it would in other markets.real estate investor

    ReplyDelete
  20. This is certainly additionally an exceedingly wonderful offer everyone truly qualified on the lookout with. It’s always not likely everyday there is chances read a little something. extremely well written article as . I will be sure to bookmark and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.Phoenix AZ Real Estate

    ReplyDelete
  21. I think I didn't write clearly. Where supply is inelastic, rents and supply are relatively unaffected by raising property taxes. Where supply is elastic, rents reflect marginal costs, so the tax falls on the consumer as it would in other markets.hiring a ghostwriter cos

    ReplyDelete

  22. These real estate agents want to play with people till they are satisfied with it. I do not know why they do that. If they really want something from their consumer, they should have just let them know about the fact rather than doing such thing.Apartamentos en Venta

    ReplyDelete
  23. Excelr is providing emerging & trending technology training, such as for data science, Machine learning, Artificial Intelligence, AWS, Tableau, Digital Marketing. Excelr is standing as a leader in providing quality training on top demanding technologies in 2019. Excelr`s versatile training is making a huge difference all across the globe. Enable ?business analytics? skills in you, and the trainers who were delivering training on these are industry stalwarts. Get certification on "data science training institutes in hyderabad"and get trained with Excelr. Chip Eng Seng Real Estate Developer

    ReplyDelete
  24. We actually just updated our trust this year–we had created it years ago, but since then a lot has changed, including having another child, selling our house and buying another one, and the couple that we had designated as guardians getting a divorce–so it was definitely time. The relief of having it all up-to-date now is huge.apartments for rent in dubai

    ReplyDelete
  25. Very interesting. Property taxes are probably the way to go. I do not see much traction on this score.jacksonville locksmith

    ReplyDelete
  26. Great post on REITS! I have invested in a retirement home REIT, and it’s the only one of my investments that has not gone down since the market started doing badly. Sooo, best decision i think i could have made! Definitely interested in looking into another one though, since buying a house is not really an option for me right now. Thanks for sharing this!!raleigh roofing

    ReplyDelete
  27. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Real estate

    ReplyDelete
  28. Your commercial tenant failed to pay rent. You have heard that things are not going very well for them, but now it is apparent. As a property manager your duty and obligation is to resolve the issue as quickly as possible. When the tenant failed to pay by the due date they have effectively breached the lease and you are entitled to evict the tenant from the property. property management

    ReplyDelete