Felix Scr change log levels - osgi

I am using Apache ServiceMix 7.0.1 with Felix SCR. On startup, a number of my OSGI components are in Disabled / Unsatisfied state. In order to get some additional logs around the lifecycle of the components, I looked at changing the log levels for scr and found this article -
http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
But I am not clear on where to set the ds.loglevel property in ServiceMix. I tried setting the same in config.properties and also by passing as a launch argument via -D option but it did not result in any additional logging.
Can you please advise me on how to troubleshoot the failing components?
Thanks.

As far as I know scr is using the OSGi log service. In many log configurations these logs are not forwarded to the log backend.
I recently found that felix now offers a new logging solution based on logback that also works for log service as well as several types of OSGi events. So I propose you try with felix logback support bundle.
Here is a blog how to set it up:
http://liquid-reality.de/2018/08/07/logging-osgi.html
Edit: The blog text is below, slightly worse formatted, from a time when the link appeared to be dead because it had moved from blog.liquid-reality.de.
Logging in OSGi seemed to be an arcane thing for quite some time. On
the logback website there is still this explanation by Ekke which was
surely good 2008 but in 2018 people do not accept creating their own
logging bridges, adding config using fragments and tweaking start
levels.
Luckily this all improved quite a lot. Apache Karaf uses pax-logging
and there is now also the felix logback support bundle. In this
article I will focus on the latter as it is simple to setup and has
some nice features.
Example code
I added the felix logback support to my OSGi DS hello
world example because logging is a core aspect in any professional
development.
See the Readme in the example for instruction how to build and run it.
Logging frontends
Logback + Felix logback supports a wide range of
logging frontends (slf4j, jul, log4j, logback, commons logging, OSGi
Log service). For your own code I recommend to use the slf4j API. It
is very slim in dependencies and provides a lot of features.
At compile time you only need the slf4j API.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
You instantiate slf4j exactly like outside OSGi. So it
can also be used for hybrid code that can run inside and outside of
OSGi.
class MyClass {
Logger log = LoggerFactory.getLogger(this.getClass()); }
Deployment
At runtime you install the bundles below. These also
include the Felix log service which is used by some OSGi reference
implementations.
<dependency> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <version>1.7.25</version>
</dependency> <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.0</version> </dependency> <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.0</version> </dependency> <dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.log</artifactId>
<version>1.2.0</version> </dependency> <dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.logback</artifactId>
<version>1.0.0</version> </dependency>
The simplest way to install these is to use the bndtools bndrun packaging like in the example
above.
Configuration
The logback configuration can be provided by a framework
property. Logback will automatically watch the file for changes and
apply new settings.
-runproperties: logback.configurationFile=file:${.}/logback.xml
You can use plain logback configs but felix logback also provides some
special settings to configure OSGi specific logs like bundle events.
See the examples in the felix logback docs.
An example config can be found here.

Related

Spring: spring-data-mongodb or spring-boot-starter-data-mongodb

Which's the difference between
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
and,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
I'm developing an spring boot service.
spring-boot-starter-data-mongodb contains configuration classes for Spring Boot. It also includes the spring-data-mongodb library so you would only need to include the start in your boot app:
https://search.maven.org/artifact/org.springframework.boot/spring-boot-starter-data-mongodb/2.0.5.RELEASE/jar
spring-boot-starter-data-mongodb is a spring boot starter pom. For more information on starters:
spring-boot-starters
Dependency management is a critical aspects of any complex project. And doing this manually is less than ideal; the more time you spent on it the less time you have on the other important aspects of the project.
Spring Boot starters were built to address exactly this problem. Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.

Spring framework compatibility between various projects

I am learning Spring framework and while trying "various" sub-projects within this, I got this doubt.
Spring framework has "core spring" at the heart of it. Now, as the project grows, e.g. trying other features like: spring-mvc, spring-web flow , spring security etc. Are all those sub-projects part of same release. For example, if I look for spring 4.0.2 release, would all these sub-projects be included in this? (hence release for various sub-project with same number: 4.0.2).
If this is not correct, then how do we ensure to chose the compatible sub-projects?
Thanks
spring-mvc is part of the spring framework, the others are separate projects following their own versioning. In general there is a minimum version for the projects and most work fine with newer versions.
If you want to be sure use the Spring IO Platform to manage your dependencies.
In your pom add
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Then you can simply add the dependencies (without version) to your dependencies section
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
</dependencies>
For a list of managed dependencies (and version) check Appendix A of the reference guide.
Spring framework has "core spring" at the heart of it. Now, as the
project grows, e.g. trying other features like: spring-mvc, spring-web
flow , spring security etc. Are all those sub-projects part of same
release
spring-mvc and spring-web are both individual artifacts that you'll find within a single Spring release. They are versioned together, and you should always use the same version for all of them in any given project.
spring-security, however, is a completely different beast. It sits on top of Spring, but it's versioned completely separately. You need to make sure that the version of Spring Security you use is combined with a compatible version of Spring.

spring-boot-starter versus spring-boot-starter-xxx

I noticed that the Spring Boot Sample Data Redis declares the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
See here for full POM: https://github.com/spring-projects/spring-boot/blob/v1.0.0.RC4/spring-boot-samples/spring-boot-sample-data-redis/pom.xml
I see no mention of the <artifactId>spring-boot-starter-redis</artifactId>
My question is: when do I use spring-boot-starter versus spring-boot-starter-xxx where xxx is the name of the project (here Redis)?
The answer to the specific question: spring-boot-starter is a baseline for the others, and for standalone (non-web) apps that don't use any other Spring components - it has basic support for Spring, Logging, and Testing, but nothing else (no webapp features, no database etc.). Since all the other starters depend on it, once you use another one you can remove the vanilla starter. EDIT: see here https://github.com/spring-projects/spring-boot/commit/77fd127e09963a844f8fb4e574e1f0d9d3424d4e.
Up to you on the redis starter, but I would use the starter if it exists, since it will typically cut down on the number of dependencies you need to declare. The redis one actually doesn't add a lot of value (hence it didn't exist until recently), but it probably ought to be used in the sample.

Deploying Simple Spring Hibernate Utility Project via Maven to Tomcat

My goal is understanding the J2EE lifecycle at a high-level with Spring, Hibernate, and Maven. From much research, I understand that Spring provides dependency injection and Hibernate provides object-relation mapping with databases. Maven is a tool to improve the build/deployment process from my understanding. With that said, everywhere I search I get more and more lost on configuration files (i.e. pom.xml, server.xml, etc.), terminology, and alternatives such as Gradle. I just want to build and launch the application and be able to see via http://localhost:8080 in tomcat.
At first, I couldn't get the default project (picture attached) built, but after further research found that I needed to Maven clean and Maven install.
I also modified settings in pom.xml changing version numbers and the database to use MySQL.
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
The next issue I had was in OrderPersistenceTests.java it used org.hibernate.classic.Session which is no longer the correct class path and found that it needed to be org.hibernate.Session.
Finally, I was able to get it to build but cannot figure out how to deploy to Tomcat from Spring Tool Suite.
I have put together a simple example using Maven, Spring, Hibernate, and ExtJS for the front end at the following link:
https://github.com/drembert/java-webapp-sample
If you are using Spring Source Tool Suite which it looks like you are in the screen shot, you should be able to import everything using the "Import Maven Projects" option. The example uses Hypersonic as the in-memory DB to allow easier deployment. Keep in mind this example generates two different .war files (one is presentation-layer and the other is service-layer) to emulate a simple RESTful service so both will need to be deployed to the Spring tcServer (STS's version of Tomcat), but once they are you should be able to view the GUI at http://localhost:8080/presentation-layer. Another thing to make note of is that this example currently doesn't have a security layer which would normally be implemented using Spring security, but I am working on adding that in the near future.

Osgi with Java EE 6 Getting started

I am trying to develop a service where in all the different stacks e.g Persistence, Security, etc. run as OSGi bundles in an OSGi container. I am using JBoss AS 7.1.1 as my OSGi container.
I used the following JBoss stack to setup my project.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.bom</groupId>
<artifactId>jboss-javaee-6.0-with-osgi</artifactId>
<version>1.0.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
However, I was thinking the Manifiest.MF as well as activator classes will be auto-generated (not that i have a problem doing this), but to my surprise, nothing happens and I am bumped on that.
Also i cannot figure out how to wire services provided by different OSGi bundles together in a web application.
Most of the examples I see are targeting a Spring environment. Please, could someone point me in the Java EE 6 direction?
Your best bet for wiring is probably Blueprint. Blueprint can also replace Activators with eager singleton beans. Blueprint can't be used directly within the web bundle to register and consume services, but a JNDI-Blueprint bridge allows you to use JNDI lookups with an 'osgi:service' namespace.
You can find plenty of Blueprint and Enterprise OSGi tutorials if you google for Apache Aries, and you may find Enterprise OSGi in Action useful. Chapters 1 and 2 are available free on the web.

Resources