Eclipse (Java & Hibernate) Content Assistant Not Working - java

I'm having a problem with Eclipse and the content assistant regarding Hibernate.
As far as I understand, I've linked the Javadoc for hibernate-core-4.0.0.CR7.jar correctly (the validation goes through).
import org.hibernate.cfg.Configuration;
public class TestHibernate
{
Configuration config = new Configuration();
config.
}
Once I typed "config." however, I was not greeted with the content assistant. Can anyone offer some insight into what might be missing? Thanks!

You are not writing in a method body or an initialisation block.
import org.hibernate.cfg.Configuration;
public class TestHibernate
{
Configuration config = new Configuration();
public void someMethod() {
config. //Should work from here
}
}

Related

how to run Camel Main so it will keep running, and also use Spring Context XML DSL?

I'm upgrading an old system that was a batch job that used Camel Main to continue running, so that it can basically loop and query a database every few seconds. It also uses Spring for configuration, but doesn't use Spring Boot. It was on Camel 2.x and I'm having to upgrade it to Camel 3.14. The Main class has changed in that time. In addition to being moved to a different package, it has lost the method it was using to add the Spring context, which was setApplicationContextUri("app-context"). There is a configure() method on Main now, but I still don't see a way of adding a Spring context to Main.
Looking at javadocs for the new Main, I see there are methods in MainSupport that reference CamelContext, but they seem to be about creating a blank CamelContext. There is also an autoconfigure(CamelContext) which takes in CamelContext, but it's protected, so I don't see how to call it. I guess without extending Main, which I don't see any use cases or examples for.
Alternatively, if there's a way to do this without using Main, I'm open to that as well.
The Spring and CamelContext are mainly used to set up beans like dataSources and Properties. The Route is defined in the same class that contains the java main() method that is called from the script used to start the whole process (this is the old version):
package com.foo.email.ffdb.listener;
import java.util.Properties;
import javax.annotation.Resource;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.foo.email.ffdb.util.FireFrgtConstants;
public class EmailDBListener extends RouteBuilder {
private static Logger log = LogManager.getLogger(EmailDBListener.class.getName());
private static String routeId = FireFrgtConstants.EMAIL_ROUTE_ID;
#Autowired
private EmailDBProcessor emaiDBProcessor;
#Resource
private Properties emailProperties;
public static void main(String[] args) throws Exception {
System.out.println("STARTING EMAILDBLISTENER");
log.debug("Starting Email Batch ");
Main main = new Main();
main.setApplicationContextUri("app-context.xml");
main.run();
log.info("Email Batch Started:");
}
#Override
public void configure() throws Exception {
log.debug("configure() ");
from(configureSqlTimer())
.routeId(routeId)
.to("sqlComponent:{{SQL.READ_EMAIL_REQUESTS}}")
.bean("fireForgetServiceMapper", "readEmailRequests")
.process(emaiDBProcessor);
}
private String configureSqlTimer() {
log.debug("configureSqlTimer() ");
String pollingTime = emailProperties.getProperty(FireFrgtConstants.POLLING_TIME);
String sqlTimer = "timer://pollFireFrgtTable?period=" + pollingTime + "s";
return sqlTimer;
}
}
I just had the wrong Main. There is one in camel-main, and another in camel-spring-main. I just needed to use the camel-spring-main, and it started running and staying alive.
Except for a transaction problem I am creating another question for...
But the main program is running

Adding Apache Camel custom component/endpoint in a Spring application

I'm trying to implement a custom endpoint in a Spring Boot application.
Goal is to use routes as: from("...").process("...").to("my:...");
Now, I have 3 classes: a DefaultConsumer, a DefaultEndpoint, a DefaultComponent:
package com.my.endpoint;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.support.DefaultEndpoint;
public class MyEndpoint extends DefaultEndpoint {
public MyEndpoint(String uri, MyComponent myComponent) {
}
...
}
package com.my.endpoint;
import org.apache.camel.Endpoint;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Endpoint endpoint, Processor processor) {
super(endpoint, processor);
}
}
package com.my.endpoint;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
import java.util.Map;
#Component("my")
public class MyComponent extends DefaultComponent {
public MyComponent(CamelContext camelContext) {
super(camelContext);
}
...
}
Now: how can I register?
In a Spring configuration class, I have:
#Override
public void configure() throws Exception {
camelContext.addComponent("my", new MyComponent(camelContext));
But is not working:
Caused by: org.apache.camel.NoSuchEndpointException: No endpoint could be found for: my, please check your classpath contains the needed Camel component jar.
So, I added the META-INF file in services/org/apache/camel/component/my:
class=com.my.endpoint.MyComponent
But also this, is not working.
There is no complete tutorial on how to implement this.
Any help?
Note: I'm trying to implement an Endpoint because I need to integrate my systems using my data types. I tried using Transformer but failed because of this: Set a custom DataType in Apache Camel Processor
Before, I tried using data type converter, but failed because of this (marked duplicate because people are too lazy to really understand questions): Enforce type conversion on Rest consumer in Apache Camel
I've FULLY read "Apache Camel In Action, Second Edition" but, at the moment, I can't continue with my project because of?
This is because custom component must be annotated by #UriEndpoint annotation.
Another way to solve this problem: Set EndpointUri via Constructor or by implementing createEndpointUri() in MyEndpoint.
So easiest way might be changing your constructor to:
public MyEndpoint(String uri, MyComponent myComponent) {
super(uri, myComponent);
}

JOOQ is not generating classes

I have a problem with JOOQ framework (3.13.4) along with Spring Boot and Java 8.
The problem is that I'm trying to generate domain classes using java code way (instead of using codegen plugin with maven which had some troubles with custom naming strategy provider). So as first let me show You the #Configuration class which contains (at least I believe that it contains) all of the necessary beans:
import com.ormtester.common.base.Measurer;
import com.ormtester.common.utils.enums.OrmType;
import com.ormtester.datasources.config.RouteableDataSource;
import org.jooq.SQLDialect;
import org.jooq.codegen.GenerationTool;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.jooq.util.xml.jaxb.Schema;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.jooq.meta.jaxb.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.PostConstruct;
import java.util.Properties;
#Configuration
#EnableTransactionManagement
public class JooqConfigurator {
private Properties moduleProperties;
private RouteableDataSource routeableDataSource;
public JooqConfigurator(RouteableDataSource routeableDataSource) {
this.routeableDataSource = routeableDataSource;
try {
moduleProperties = new Properties();
moduleProperties.load(JooqConfigurator.class.getClassLoader()
.getResourceAsStream("jooq.properties"));
} catch (Exception ignore) {}
}
#Bean
public DataSourceConnectionProvider connectionProvider() {
return new DataSourceConnectionProvider(routeableDataSource);
}
#Bean
public ExceptionTranslator exceptionTransformer() {
return new ExceptionTranslator();
}
#Bean
public DefaultConfiguration configuration() {
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
jooqConfiguration.set(connectionProvider());
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
jooqConfiguration.set(SQLDialect.DEFAULT);
return jooqConfiguration;
}
#Bean
public DefaultDSLContext dsl() {
return new DefaultDSLContext(configuration());
}
#PostConstruct
public void generateCode() {
try {
GenerationTool.generate(new org.jooq.meta.jaxb.Configuration()
.withJdbc(new Jdbc()
.withDriver("com.mysql.cj.jdbc.Driver")
.withUrl("jdbc:mysql://localhost:3306/ormtester?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC")
.withUser("root")
.withPassword("root123"))
.withGenerator(new Generator()
.withName("org.jooq.codegen.JavaGenerator")
.withStrategy(new CustomStrategyProvider())
.withDatabase(new Database()
.withName("org.jooq.meta.mysql.MySQLDatabase")
.withIncludes(".*")
.withExcludes("")
.withSchemata(new SchemaMappingType().withInputSchema("ormtester").withOutputSchema("ormtester"))
.withInputCatalog("ormtester")
.withOutputCatalog("ormtester"))
.withTarget(new Target()
.withPackageName("com.ormtester.jooq.domain")
.withDirectory("jooq/src/main/java"))));
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
RouteableDataSource is a type that extends AbstractRoutingDataSource because in this case I need to have a possibility to change datasource at runtime. This thing is working well in the other regions of the project (or in another words with tools like Hibernate or MyBatis).
As You can see there is a #PostConstruct method which is used for generating domain classes and the problem is that this method doesn't generate any error or something but the classes are also not generated. I've tried to run it using PostgreSQL and Oracle database (of course changing the driver, database name etc.) and the situation is looking exactly the same.
One interesting thing is that when I'm running this code and package com.ormtester.jooq.domain is present - during the method execution domain package is getting removed.
I'd also like to mention that JOOQ autoconfiguration is disabled by excluding JooqAutoConfiguration class through the #SpringBootApplication annotation located at the project's main (starter) class.
IDE is running in administrator's mode and - what can be also interesting - if I will set the breakpoint in the getJavaClassName() method in my custom naming strategy provided (CustomStrategyProvider which extends DefaultGeneratorStrategy class, the breakpoint is reached everytime this method is used.
So does anyone faced the same problem and/or simply can tell me if I'm doing something wrong or something is missing in the code snippet that I've provieded here? I have this problem since about 4 days and now I'm running out of the ideas what can be wrong. I went through the tons of topics on many forums and nothing helped me, including the tutorials on the author's page (which in my opinion simply lacks of important informations).
I'll be really grateful for every help - thanks in advance!
Code generation is a build task, not a runtime task. I can't think of a reasonable scenario where generating code only at runtime would make sense.
The problem is that I'm trying to generate domain classes using java code way (instead of using codegen plugin with maven which had some troubles with custom naming strategy provider)
You have to create a separate maven module (or project) where you build the custom naming strategy, and then add that as a dependency to the jOOQ code generation plugin. This works the same way as with the JPADatabase, where entities have to be placed in a separate maven module.

Netflix archaius cannot read updated property file value

I am a very much new to Netflix archaius. I have a code snippet which reads Java property file and prints property value.
When this program runs it prints the value of property named "Fields" from testproperty.properties file. Now while this program is running I am updating the value of "Fields" property, so archaius should fetch change value dynamically. But it is still printing older value.
What is the correct way to use archaius with this Java Program? Or to update properties in a program without restarting it ? If someone can point out correction in this code snippet it would be helpful.
I want to run a demo with Netflix archaius, so I have imported archaius through maven in my project.
Now I am updating my properties file. But still it prints the old property value. (P.S.: I have kept the continuous while loop in driver program to see if archaius picks the update property value runtime. I guess that's what archaius suppose to do. Fetching the updated property without restarting application. Correct me if I am wrong.)
Below is my code snippet :
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
public class PropertyChangetest {
public static void main(String args[]) {
DynamicPropertyFactory sampleProp = DynamicPropertyFactory.getInstance();
System.setProperty("archaius.configurationSource.defaultFileName", "TestProperty.properties");
System.setProperty("archaius.fixedDelayPollingScheduler.delayMills", "500");
while(true) {
DynamicStringProperty sampleProp1 = sampleProp.getStringProperty("fields","");
System.out.println(sampleProp1.get());
}
}
}
My "TestProperty.properties" file only have one property called fields. After running the program, I am updating my property file but it still prints older value.
The idea is to implement a custom PolledConfigurationSource, so Archaius can poll the source and update the property for consumption. I have also included a callback that the smart way to consume the property without your App polling it again (remember Archaius is doing the polling part for you).
Important note on the sample code : The program exits after the first callback. If you want to test more callbacks, increase the counter at class variable 'latch'
package com.test.config;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import com.netflix.config.AbstractPollingScheduler;
import com.netflix.config.ConcurrentMapConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
public class TestArchaius {
CountDownLatch latch = new CountDownLatch(1);
#Test
public void tes() throws Exception {
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 1000, false);
DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(new MyPolledConfigurationSource(), scheduler);
ConfigurationManager.install(dynamicConfiguration);
DynamicStringProperty fieldsProperty = DynamicPropertyFactory.getInstance().getStringProperty("fields", "");
fieldsProperty.addCallback(() -> {
System.out.println(fieldsProperty.get());
latch.countDown();
});
latch.await();
}
class MyPolledConfigurationSource implements PolledConfigurationSource {
#Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
ConcurrentMapConfiguration configFromPropertiesFile = new ConcurrentMapConfiguration(
new PropertiesConfiguration("TestProperty.properties"));
Map<String, Object> fullProperties = new HashMap<String, Object>();
configFromPropertiesFile.getProperties().forEach((k, v) -> fullProperties.put((String) k, v));
return PollResult.createFull(fullProperties);
}
}
}
I ran into this issue recently. I wanted to implement a use case such as the DynamicPropertyFactory's prop should be updated via a REST API request. I had googled a lot on how to do that since the property was not getting updated after using the
.setProperty(String prop, String value) method. But I found that if a property is not defined before then the property gets added with the help of this method.
So the problem comes down like its not possible to override a property.
Also I found that it is using ConcurrentCompositeConfiguration for the Config instance and not the ConcurrentMapConfiguration. As I googled further found tabnine posts and found there is a method called
setOverrideProperty(java.lang.String key, java.lang.Object finalValue)
Override the same property in any other configurations in the list.
So casting to this class setting the override property resolved the issue. Property got updated successfully.
#Reference to ConcurrentCompositeConfiguration - http://netflix.github.io/archaius/archaius-core-javadoc/com/netflix/config/ConcurrentCompositeConfiguration.html

Spring AOP - why do i need aspectjweaver?

i wrote a very simple Aspect with Spring AOP. It works, but i have some problems understanding what is really going on. I don't understand why i have to add the aspectjweaver.jar? The Spring-AOP documentation clearly states that i don't need aspectj compiler or weaver as long as i just use Spring-AOP:
The AOP runtime is still pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.
My configuration looks like this:
<aop:aspectj-autoproxy />
#Aspect
#Service
public class RemoteInvocationAspect {
#Before("execution(* at.test.mypackage.*.*(..))")
public void test() {
System.out.println("test");
}
...
I also tried XML configuration, didn't change anything though. Maybe i could just let it go, but i really would like to understand why aspectj-weaver is used? If i don't add the dependency in maven i get java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
Spring AOP implementation I think is reusing some classes from the aspectj-weaver. It still uses dynamic proxies - doesn't do byte code modification.
The following comment from the spring forum might clarify.
Spring isn't using the AspectJ weaver in this case. It is simply
reusing some of the classes from aspectjweaver.jar.
-Ramnivas
You are using AspectJ style pointcut-expression #Aspect and #Before are part of AspectJ. Check this link.
Regarding the AspectJ-weaver, its actually a bytecode weaver which weaves aspects into classes at load time.
I recently had a similar question Why does spring throw an aspectj error if it does not depend on aspectj?
To use Spring AoP without an AspectJ dependency it must be done in xml. The annotations are a part of AspectJ.
Also, the really cool expression language is only supported by AspectJ. So you have to define explicit point-cuts. See Section 6.3.2. Declaring a pointcut:
http://static.springsource.org/spring/docs/2.0.x/reference/aop.html section
I'm still having trouble finding any elaborate documentation on this technique.
You need the aspectjtools or the aspectjweaver dependencies when you use the AspectJ pointcut expression language.
Please see the following classes:
Foo.java
public interface Foo {
void foo();
void baz();
}
FooImpl.java
public class FooImpl implements Foo {
#Override
public void foo() {
System.out.println("Foo!");
}
#Override
public void baz() {
System.out.println("Baz!");
}
}
MethodBeforeAdviceBarImpl.java
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MethodBeforeAdviceBarImpl implements MethodBeforeAdvice {
#Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Bar!");
}
}
And please see App.java version - 1
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
public class App {
public static void main(String[] args) {
final MethodBeforeAdvice advice = new MethodBeforeAdviceBarImpl();
final NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor = new NameMatchMethodPointcutAdvisor();
nameMatchMethodPointcutAdvisor.setMappedName("foo");
nameMatchMethodPointcutAdvisor.setAdvice(advice);
final ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addAdvisor(nameMatchMethodPointcutAdvisor);
final Foo foo = new FooImpl();
proxyFactory.setTarget(foo);
final Foo fooProxy = (Foo) proxyFactory.getProxy();
fooProxy.foo();
fooProxy.baz();
}
}
The output of running this example will be:
Bar!
Foo!
Baz!
I only need the org.springframework:spring-context.jar in my classpath. Now instead of a NameMatchMethodPointcutAdvisor, lets use AspectJExpressionPointcutAdvisor:
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
public class App {
public static void main(String[] args) {
final MethodBeforeAdvice advice = new MethodBeforeAdviceBarImpl();
final AspectJExpressionPointcutAdvisor aspectJExpressionPointcutAdvisor = new AspectJExpressionPointcutAdvisor();
aspectJExpressionPointcutAdvisor.setAdvice(advice);
aspectJExpressionPointcutAdvisor.setExpression("execution(void biz.tugay.spashe.Foo.foo())");
final ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addAdvisor(aspectJExpressionPointcutAdvisor);
final Foo foo = new FooImpl();
proxyFactory.setTarget(foo);
final Foo fooProxy = (Foo) proxyFactory.getProxy();
fooProxy.foo();
fooProxy.baz();
}
}
Again, if I only have the spring-context.jar in my classpath, I will get:
An exception occured while executing the Java class. null: InvocationTargetException: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
When you investigate the AspectJExpressionPointcutAdvisor class, you will see that it extends AbstractGenericPointcutAdvisor and which delegates the work to an instance of AspectJExpressionPointcut. And you can see that AspectJExpressionPointcut has the following import statements:
import org.aspectj.weaver.patterns.NamePattern;
import org.aspectj.weaver.reflect.ReflectionWorld.ReflectionWorldException;
import org.aspectj.weaver.reflect.ShadowMatchImpl;
import org.aspectj.weaver.tools.ContextBasedMatcher;
import org.aspectj.weaver.tools.FuzzyBoolean;
import org.aspectj.weaver.tools.JoinPointMatch;
import org.aspectj.weaver.tools.MatchingContext;
import org.aspectj.weaver.tools.PointcutDesignatorHandler;
import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParameter;
import org.aspectj.weaver.tools.PointcutParser;
import org.aspectj.weaver.tools.PointcutPrimitive;
import org.aspectj.weaver.tools.ShadowMatch;
You will need the aspectjtools dependency in your classpath in runtime so AspectJExpressionPointcut can load the classes it needs.
You can browse spring website and find the answer on page of docs.spring.io
The #AspectJ support can be enabled with XML or Java style configuration. In either case you will also need to ensure that AspectJ’s aspectjweaver.jar library is on the classpath of your application (version 1.6.8 or later). This library is available in the 'lib' directory of an AspectJ distribution or via the Maven Central repository.

Categories

Resources