I am reading Spring Framework reference documentation (version 5.0.0.M1), page 70:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;
public final class Boot {
public static void main(final String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
Messenger messenger = (Messenger) ctx.getBean("messenger");
System.out.println(messenger);
}
}
I try adding:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
but cannot resolve dependency. Where to find dependency has org.springframework.scripting.Messager?
Related
I am pretty new to Spring Boot, Apache Camel and the ActiveMQ broker. I am trying to create an application which will send a message to a queue which I am hosting locally using Camel for routing.
POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.22.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>3.2.0</version>
</dependency>
MsgRouteBuilder:
public void configure() throws Exception {
from("direct:firstRoute")
.setBody(constant("Hello"))
.to("activemq:queue:myQueue");
}
application.yaml:
activemq:
broker-url: tcp://localhost:61616
user: meAd
password: meAd
MainApp.java:
package me.ad.myCamel;
import org.apache.camel.CamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import me.ad.myCamel.router.MessageRouteBuilder;
#SpringBootApplication
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
#EnableAspectJAutoProxy(proxyTargetClass = true)
#EnableCaching
public class MeAdApp implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(MeAdApp.class);
public static void main(String[] args) {
try {
SpringApplication.run(MeAdApp.class, args);
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
}
}
#Override
public void run(String... args) throws Exception {
LOG.info("Starting MeAdApp...");
}
}
MyController.java :
#GetMapping(value = "/routing")
public boolean sendToMyQueue() {
sendMyInfo.startRouting();
return true;
}
SendMyInfo.java :
MsgRouteBuilder routeBuilder = new MsgRouteBuilder();
CamelContext ctx = new DefaultCamelContext();
public void startRouting(){
try {
ctx.addRoutes(routeBuilder);
ctx.start();
Thread.sleep(5 * 60 * 1000);
ctx.stop();
}
catch (Exception e) {
e.printStackTrace();
}
}
So, whenever I call my rest end point: /routing, I get the error:
java.lang.NoSuchMethodError: org.apache.camel.RuntimeCamelException.wrapRuntimeException(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;`
Can anybody please point me to the right direction as to why I am getting this error? Any help is greatly appreciated .
You need to have the components of the same version. If you are using camel-core with 3.2.0, use camel-activemq 3.2.0. And, since you are using spring-boot, you can make use of the starter dependencies. Just add these and you are good to go.
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-activemq-starter</artifactId>
<version>3.2.0</version>
</dependency>
com.mongodb.MongoClient //is not exists
public class MonGoDemo {
public static void main(String[] args) {
//mongodb Connection Client.
MongoClient mongoClient = new MongoClient();
}
}
First make sure that you have a dependency in your pom.xml :
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.0</version>
</dependency>
Second to use com.mongodb.MongoClient you have in your class you have to import it like so :
import com.mongodb.MongoClient;
public class MonGoDemo {
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient();
}
}
I am trying to launch an initial test of a Vertx.io server, but I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/vertx/java/core/Handler
Code:
package com.company;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
import org.vertx.java.core.VertxFactory;
import org.vertx.java.core.http.HttpServerRequest;
import java.io.IOException;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private Object shutdownLock = new Object();
public Main() throws IOException, InterruptedException {
start(1234);
keepServerFromShuttingDown();
}
private void keepServerFromShuttingDown() throws InterruptedException {
synchronized (shutdownLock) {
shutdownLock.wait();
}
log.info("Shutting down");
}
public void start(int port) {
Vertx vertx = VertxFactory.newVertx();
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
#Override
public void handle(HttpServerRequest request) {
}
}).listen(port);
}
public static void main(String[] args) throws IOException, InterruptedException {
new Main();
}
}
pom.xml:
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-platform</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
It looks like a basic CLASSPATH issue where it is not able to find Vertx classes while executing your program. Please check if the vertx libraries are indeed a part of your CLASSPATH.
Though unrelated, but if you are checking out Vertx for some new projects, I highly recommend version 3.0 and you could start with this simple maven project example
I am bootstrapping a new project from the Accessing Neo4j Data with REST example. The example uses an embedded database rather than a standalone neo4j server, but I would like to use the Neo4J webadmin interface for visualisation of my data. How do I enable the webadmin interface starting from this configuration?
(They got WrappingNeoServerBootstrapper working in use WrappingNeoServerBootstrapper with spring-data-neo4j but a lot of knowledge is omitted from the answer, e.g. it is not even mentioned where to place to the configuration. Being new to POMs, Spring Boot and Neo4j I therefore can't make use of that answer.)
The example you are using needs some tweaking to enable the Neo4j browser. I started from a different example, the Accessing Data with Neo4j example and it worked well.
You will need to do the following:
Change the version on your spring boot pom to 1.2.1.Release:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
Add dependencies for Neo4jServer:
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.1.5</version>
<classifier>static-web</classifier>
</dependency>
Implement the Spring Boot command line runner in your Application.class:
public class Application extends Neo4jConfiguration implements CommandLineRunner{
Autowire a reference to your GraphDatabaseService in your Application.class:
#Autowired
GraphDatabaseService db;
#Override the run method from CommanLineRunner in your Application.class:
#Override
public void run(String... strings) throws Exception {
// used for Neo4j browser
try {
WrappingNeoServerBootstrapper neoServerBootstrapper;
GraphDatabaseAPI api = (GraphDatabaseAPI) db;
ServerConfigurator config = new ServerConfigurator(api);
config.configuration()
.addProperty(Configurator.WEBSERVER_ADDRESS_PROPERTY_KEY, "127.0.0.1");
config.configuration()
.addProperty(Configurator.WEBSERVER_PORT_PROPERTY_KEY, "8686");
neoServerBootstrapper = new WrappingNeoServerBootstrapper(api, config);
neoServerBootstrapper.start();
} catch(Exception e) {
//handle appropriately
}
// end of Neo4j browser config
}
When you are all done, your Application.class should look like this:
package hello;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.server.WrappingNeoServerBootstrapper;
import org.neo4j.server.configuration.Configurator;
import org.neo4j.server.configuration.ServerConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
#Configuration
#EnableNeo4jRepositories
#Import(RepositoryRestMvcConfiguration.class)
#EnableAutoConfiguration
public class Application extends Neo4jConfiguration implements CommandLineRunner{
public Application() {
setBasePackage("hello");
}
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("target/hello.db");
}
#Autowired
GraphDatabaseService db;
#Override
public void run(String... strings) throws Exception {
// used for Neo4j browser
try {
WrappingNeoServerBootstrapper neoServerBootstrapper;
GraphDatabaseAPI api = (GraphDatabaseAPI) db;
ServerConfigurator config = new ServerConfigurator(api);
config.configuration()
.addProperty(Configurator.WEBSERVER_ADDRESS_PROPERTY_KEY, "127.0. 0.1");
config.configuration()
.addProperty(Configurator.WEBSERVER_PORT_PROPERTY_KEY, "8686");
neoServerBootstrapper = new WrappingNeoServerBootstrapper(api, config);
neoServerBootstrapper.start();
} catch(Exception e) {
//handle appropriately
}
// end of Neo4j browser config
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The Neo4j browser will be available on the host and port configured in your run() method.
I'm trying to use google guice for dependency injection however I can't seem to wire everything togheter.
In my web.xml I defined the guiceFilter and the guiceListener like so:
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>backend.listener.GuiceConfigListener</listener-class>
</listener>
the config listener is basicly pretty simple:
#Override
protected Injector getInjector(){
return Guice.createInjector(new ServletModule(), new ArtsModule());
}
and the ArtsModule at this moment just has one binding like so:
#Override
protected void configure(){
bind(ArtsDAO.class).to(ArtsDAOGae.class);
}
I then continue to do a field injection of the ArtsDao in a service class:
#Inject
private ArtsDAO artsDAO;
But when I try to build my project (which is a maven build) I get a NPE on the artsDAO field, this most likely happens because the unit tests aren't running in a web environment.
Can anyone advice me on how to configure the guice bidings so that they are picked up during unit testing?
Thanks
Pip,
this is not trivial task but definitely you can achieve what you want.
First of all have a look at Tadedon project at https://code.google.com/p/tadedon
especially tadedon-guice-servlet-mock.
You will need something like fake container for your test. My fake container contains also Apache Shiro integration so you can throw it out, It looks like:
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainer;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerModule;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Arrays;
public class FakeTestContainerInit {
private final FakeServletContainer servletContainer;
private final Injector internalInjector;
private Subject internalSubject;
public FakeTestContainerInit() {
this(new Module[] {});
}
public FakeTestContainerInit(Module... modules) {
super();
modules = Arrays.copyOf(modules, modules.length + 1);
modules[modules.length-1] = new FakeServletContainerModule();
internalInjector = Guice.createInjector(modules);
servletContainer = internalInjector.getInstance(FakeServletContainer.class);
}
public void start() throws ServletException, IOException {
this.start(true);
}
public void start(boolean initializeSecurityContext) throws ServletException, IOException {
getServletContainer().start();
MockHttpServletRequest request = servletContainer.newRequest("GET","/");
MockHttpServletResponse response = new MockHttpServletResponse();
if(initializeSecurityContext) {
SecurityManager scm = internalInjector.getInstance(SecurityManager.class);
internalSubject = new WebSubject.Builder(scm, request, response).buildWebSubject();
SubjectThreadState sts = new SubjectThreadState(internalSubject);
sts.bind();
} else { internalSubject = null; }
getServletContainer().service(request, response);
}
public void stop() {
servletContainer.stop();
}
public FakeServletContainer getServletContainer() {
return servletContainer;
}
public <T> T getInstance(final Class<T> type) throws IOException, ServletException {
return getServletContainer().getInstance(type);
}
public <T> T getInstance(final Key<T> key) throws IOException, ServletException {
return getServletContainer().getInstance(key);
}
public Subject getSubject() {
return internalSubject;
}
}
Dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
</dependency>
<dependency>
<groupId>com.xemantic.tadedon</groupId>
<artifactId>tadedon-guice-servlet-mock</artifactId>
</dependency>
and Apache Shiro you won't need:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</dependency>
All you need to do, is create FakeTestContainerInit and call start() and stop() method. Also all object creations have to be done via FakeTestContainerInit.getInstance method inside tests.
Well, I used it to test Vaadin application so I did not need sending requests and checking responses. So, this one you will need to implement. It can be done via getServletContainer().service(request, response);. But i think you will figure out. Hope it will help you.