Cannot Autowire and run Spring web application.
Error:
java.lang.IllegalStateException: Autowired annotation requires at least one argument: public main.java.com.springapp.mvc.controller.DSLRServletController()
DSLRServletController:
package main.java.com.springapp.mvc.controller;
import main.java.com.springapp.mvc.dao.DSLRDAO;
import main.java.com.springapp.mvc.model.DSLR;
import main.java.com.springapp.mvc.pckg.DSLRForm;
import main.java.com.springapp.mvc.pckg.DSLRValidaor;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
#Controller
public class DSLRServletController {
static Logger logger = Logger.getLogger(DSLRServletController.class);
private DSLR DSLR;
private DSLRDAO dslrDAO;
private DSLR dslr;
#Autowired
public DSLRServletController() {
this.dslrDAO = new DSLRDAO();
}
public void init() {logger.error("DSLRServlet.init(): just started"); }
#RequestMapping(value = "/s", method = RequestMethod.GET)
public String showHTMLResponse(#ModelAttribute("dslrs") DSLR dslrs[],
#ModelAttribute("dslr") DSLR dslr,
#ModelAttribute("dslrErrors") HashMap dslrErrors,
#ModelAttribute ("dslrform") DSLRForm dslrForm,
#RequestParam("id") String paramId,
#RequestParam("action") String paramAction,
Model model){
if(paramId == null || paramId.equals("")){
//show_all_dslrs
dslrs = getAllDslrs(); // DSLR adslrs[] -> to MODEL; HOW?
return "dslrs";
}else{
//show_this_dslr;
HashMap<String,Object> dslrHashMap = getDSLRById(paramId);
dslr = (DSLR) dslrHashMap.get("dslr");
dslrForm = (DSLRForm)dslrHashMap.get("dslrForm");
dslrErrors = (HashMap)dslrHashMap.get("dslrErrors");
if(dslr != null){
return "dslr";
}
else{
return "error";
}
}
}
#RequestMapping(value = "/s", method = RequestMethod.POST)
public String showHTMLResponsePOST(#ModelAttribute("dslrs") DSLR dslrs[],
#ModelAttribute("dslrErrors") HashMap<?,?> dslrErrors,
#ModelAttribute ("dslrform") DSLRForm dslrForm,
#RequestParam("id") String paramId,
#RequestParam("action") String paramAction,
#RequestParam("dslr_model") String paramModel,
#RequestParam("price") String paramPrice,
#RequestParam("description") String paramDescription,
Model model){
int iStatusCode = 0;
if(paramAction.equals("save") )
iStatusCode = saveDSLR(paramId, paramModel, paramPrice, paramDescription, dslrErrors, dslrForm); // POST
return "dslrs";
}
private int saveDSLR(String paramId,
String paramModel,
String paramPrice,
String paramDescription,
HashMap<?,?> context_dslrErrors,
DSLRForm context_dslrForm
) {
int byte0 = 1;
try {
DSLRValidaor dslrValidaor = new DSLRValidaor();
DSLRForm dslrForm = new DSLRForm();
dslrForm.setDslrId(paramId);
dslrForm.setModel(paramModel);
dslrForm.setPrice(paramPrice);
dslrForm.setDescription(paramDescription);
HashMap hashmap = dslrValidaor.Validate(dslrForm);
if(hashmap.size() > 0) {
context_dslrForm = dslrForm;
context_dslrErrors = hashmap;
byte0 = -1;
} else{
DSLRDAO planedao = new DSLRDAO();
DSLR dslr = new DSLR();
dslr.setDslrId(Integer.parseInt(paramId));
dslr.setModel(paramModel);
dslr.setPrice(Integer.parseInt(paramPrice));
dslr.setDescription(paramDescription);
planedao.update(dslr);
}
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("DSLRServlet.saveDSLR():").append(exception.getMessage()).toString());
byte0 = -1;
}
return byte0;
}
private DSLR[] getAllDslrs(){
DSLR adslrs[] = null;
try
{
DSLRDAO DSLRDAO = new DSLRDAO();
adslrs = (DSLR[])DSLRDAO.findAll();
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("PlaneServlet.getAllPlanes():").append(exception.getMessage()).toString());
}
// request.setAttribute("dslrs", adslrs);
return adslrs;
}
private HashMap<String, Object> getDSLRById(String s)
{
HashMap<String,Object> map = new HashMap<String, Object>();
DSLR dslr = null;
try {
int i = Integer.parseInt(s);
DSLRDAO DSLRDAO = new DSLRDAO();
dslr = (DSLR)DSLRDAO.findById(i);
DSLRForm dslrForm = new DSLRForm();
dslrForm.setDslrId(Integer.toString(dslr.getDslrId()));
dslrForm.setModel(dslr.getModel());
dslrForm.setPrice(Integer.toString(dslr.getPrice()));
dslrForm.setDescription(dslr.getDescription());
map.put("dslr", dslr);
map.put("dslrform", dslrForm);
map.put("dslrErrors", new HashMap());
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("DSLRServlet.getDSLRById():").append(exception.getMessage()).toString());
}
return map;
}
#Autowired
public void setDslrDAO(DSLRDAO dslrDAO) {
this.dslrDAO = dslrDAO;
}
public DSLRDAO getDslrDAO() {
return dslrDAO;
}
#Autowired
public void setDSLR(DSLR DSLR) {
dslr = DSLR;
}
public DSLR getDSLR() {
return dslr;
}
}
Why #Autowired annotation returns error? How to fix it?
In addition to reimeus answer, you can't use #Autowired on a default constructor.
It's often considered better to autowire the constructor rather than a field.
#Autowired
public DSLRServletController(DSLRDAO dslrDAO) {
this.dslrDAO = dslrDAO;
}
The constructor isnt a valid setter method or instance variable
#Autowired
private DSLRDAO dslrDAO;
I was facing this same issue and found the root cause, which is different than the original question asked, but may help someone for sure.
We were using Lombok jar to generate default code like Getters/Setters, similarly we were using Lombok annotation to generate a Constructor for the Required Argument and mark it with #Autowired behind the scene.
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
public class MyClass {
private MyDependency myDependency;
}
The issue which was causing the problem was a missing final keyword while defining the dependency. #RequiredArgsConstructor identifies the required argument as the one which are marked final and must be populated during construction.
Another annotation which could have been used is #AllArgsConstructor (this will create a constructor for all the dependencies so final is not really needed)
So, the correct solution to resolve the said error is to use final
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
public class MyClass {
private final MyDependency myDependency;
}
Related
package com.eukolos.restaurant.dto;
import com.eukolos.restaurant.model.Table;
import org.springframework.stereotype.Component;
#Component
public class AllTableResponseConverter {
public AllTableResponse convert(Table table ) {
AllTableResponse allTableResponse = new AllTableResponse();
allTableResponse.setId(table.getId());
allTableResponse.setNumber(table.getNumber());
allTableResponse.setAccountList(table.getAccountList().stream().map(AccountIdResponseConverter::convert).collect(Collectors.toList()));//
return allTableResponse;
}
}`
getAccountList() cant use with stream
how can i handle?
Can you test this code below? Is this what you want to achieve?
#Component
public class AllTableResponseConverter {
public AllTableResponse convert(Table table ) {
List<AccountIdResponseConverter> convertedAccounts = new ArrayList<>();
AllTableResponse allTableResponse = new AllTableResponse();
allTableResponse.setId(table.getId());
allTableResponse.setNumber(table.getNumber());
for(Table t : table.getAccountList()) {
AccountIdResponseConverter converter = new AccountIdResponseConverter();
convertedAccounts.add(converter.convert(t));
}
allTableResponse.setAccountList(convertedAccounts);
return allTableResponse;
}
}
I'm pretty new to Springboot and Java in general and because we got this in school I'm fiddeling arround.
I'm now trying to save an entity outside of the Springboot Entities, Repositories or RestController with the following code:
InfMApplication.java:
package com.domain.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.domain.springboot.repositories.MovieRepository;
import com.domain.springboot.services.MovieImport;
#SpringBootApplication
public class InfMApplication {
public static void main(String[] args) {
SpringApplication.run(InfMApplication.class, args);
MovieImport movieImport = new MovieImport();
movieImport.saveToDb();
}
}
MovieImport.java:
package com.domain.springboot.services;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.io.*;
import java.net.URL;
import com.google.gson.Gson;
import com.domain.omdbapi.entities.Movie;
import com.domain.omdbapi.entities.SearchResponse;
import com.domain.omdbapi.entities.SearchResult;
import com.domain.springboot.repositories.ComplexRepository;
import com.domain.springboot.repositories.DocumentRepository;
import com.domain.springboot.repositories.MovieRepository;
import com.domain.springboot.repositories.SimpleRepository;
#Service
public class MovieImport {
private final MovieRepository movieRepository;
public MovieImport(MovieRepository movieRepository){
this.movieRepository = movieRepository;
}
public void main() {
String randomImdbId = fetchRandomMovie();
Movie movie = fetchMovieDetails(randomImdbId);
saveToDb(movie);
}
public void saveToDb(Movie movie) {
com.domain.springboot.entities.Movie springbootMovie = new com.domain.springboot.entities.Movie(movie.Title, movie.imdbID);
this.movieRepository.save(springbootMovie);
}
public String fetchRandomMovie() {
String randomWord = getRandomWord();
String url = "https://www.omdbapi.com/?apikey=<API_KEY>&type=movie&s=" + randomWord;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(
URI.create(url))
.header("accept", "application/json")
.build();
HttpResponse<String> response = null;
try {
response = client.send(request, BodyHandlers.ofString());
} catch (Exception e) {
System.out.println(e);
}
Gson gson = new Gson();
SearchResponse searchResponse = gson.fromJson(response.body(), SearchResponse.class);
int randomIndex = new Random().nextInt(0, searchResponse.getSearch().length);
SearchResult randomResult = searchResponse.getSearch()[randomIndex];
return randomResult.getImdbID();
}
public Movie fetchMovieDetails(String imdbId) {
String url = "https://www.omdbapi.com/?apikey=<API_KEY>&type=movie&plot=full&i=" + imdbId;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(
URI.create(url))
.header("accept", "application/json")
.build();
HttpResponse<String> response = null;
try {
response = client.send(request, BodyHandlers.ofString());
} catch (Exception e) {
System.out.println(e);
}
Gson gson = new Gson();
Movie movie = gson.fromJson(response.body(), Movie.class);
return movie;
}
public String getRandomWord() {
URL resource = getClass().getClassLoader().getResource("Wordlist.txt");
List<String> words = new ArrayList<>();
try {
File file = new File(resource.toURI());
words = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
int randomIndex = new Random().nextInt(0, words.size());
return words.get(randomIndex);
}
}
If I use "this.movieRepository.save(movieObject);" to save a movie in the MovieRestController the same way, it works. I also tried adding the "#Autowire" annotation, but this didn't work.
I always get the error
java.lang.NullPointerException: Cannot invoke "com.domain.springboot.repositories.MovieRepository.save(Object)" because "this.movieRepository" is null
How can I get to use the movieRepository in other Java classes like in the RestControllers?
java.lang.NullPointerException: Cannot invoke
"com.domain.springboot.repositories.MovieRepository.save(Object)"
because "this.movieRepository" is null
Above is perfectly valid if we look at your following shared code.
public class MovieImport {
private MovieRepository movieRepository;
public void saveToDb() {
// Create movie
com.domain.springboot.entities.Movie springbootMovie = new com.domain.springboot.entities.Movie("Iron Man", "284cb8fgf");
this.movieRepository.save(springbootMovie);
}
}
You've to correct certain things in your code base.
First you're not initializing the movieRepository and therefore, you're getting the null pointer exception. As you've been using the springboot you can use construction injection to initialized the field by spring container. Also. this class should be scanned by spring and you should also put some annotation such as Component or Service on top of it.
Following will work if your MovieImport and MovieRepository classess will scan by springboot.
package com.domain;
import com.domain.omdbapi.entities.Movie;
import com.domain.springboot.repositories.MovieRepository;
#Service
public class MovieImport {
private final MovieRepository movieRepository;
public MovieImport(MovieRepository movieRepository){
this.movieRepository = movieRepository;
}
public void saveToDb() {
// Create movie
com.domain.springboot.entities.Movie springbootMovie = new com.domain.springboot.entities.Movie("Iron Man", "284cb8fgf");
this.movieRepository.save(springbootMovie);
}
}
Updated
#SpringBootApplication
public class InfMApplication implements CommandLineRunner {
#Autowired
private MovieImport movieImport;
public static void main(String[] args) {
SpringApplication.run(InfMApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
movieImport.saveToDb();
}
}
I am trying to get a value from my application.properties in Spring Boot using #Value but no matter what I do it is always null.
I am doing this in my HTTPClient test class. I have tried using environment variables, propertySource, PostConstruct, using getters and setters, and anything else I could find online but it does not seem to be populating at all... My test class is in src/test/java and the application.properties is in src/test/resources. I do also have a application.properties in my src/main/java but that is in a completely different folder so it should not be affecting it.
Here is my code:
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import com.google.gson.Gson;
import com.nulogix.billing.configuration.EndPointTestConfiguration;
import com.nulogix.billing.mockserver.MockServerApp;
#SpringBootTest(classes = EndPointTestConfiguration.class)
public class HttpClientTest {
#Value("${billing.engine.address}")
private String billingEngineAddress;
#Value("${billing.engine.port}")
private String billingEnginePort;
#PostConstruct
private void customInit() {
System.out.print(billingEngineAddress);
System.out.print(billingEnginePort);
}
public static final String request_bad = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|";
public static final String request_good = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
#Test
public void test_bad() throws ClientProtocolException, IOException {
// missing parameter
String result = Request.Post("http://" + billingEngineAddress + ":" + billingEnginePort)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_bad, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, false);
String errorMessage = (String) resultJsonObj.get("errorMessage");
assertEquals(errorMessage.contains("Payload has incorrect amount of parts"), true);
}
#Test
public void test_good() throws ClientProtocolException, IOException {
String result = Request.Post("http://" + billingEngineAddress + ":" + billingEnginePort)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_good, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), false);
assertEquals(resultJsonObj.containsKey("HasCopay"), true);
assertEquals(resultJsonObj.containsKey("CopayAmount"), true);
assertEquals(resultJsonObj.containsKey("HasCoinsurance"), true);
assertEquals(resultJsonObj.containsKey("CoinsuranceAmount"), true);
assertEquals(resultJsonObj.containsKey("version"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, true);
String version = (String) resultJsonObj.get("version");
assertEquals(version, "0.97");
}
}
I am getting the values from my application.properties to get the IP address and port and test my Request.post.
Here is my application.properties
server.port=9119
server.ssl.enabled=false
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.name=Patient_Responsibility
So what solved my issue is copying over the application.properties from my main to my test/resources. I then used #PropertySource to change the value to test.properties, separating it from application.properties in main. I created a bean of values in my endpoint config and then auto wired it into my httpclient test class.
I also added in web environment in my SpringBootTest annotation to use the defined port in test.properties and to run the test with my endpoint config class and main app class. This caused the #value strings to be populated and not be null.
#Configuration
public class EndPointTestConfiguration {
#Value("${billing.engine.address}")
private String mockServerIP;
#Value("${billing.engine.port}")
private String mockServerPort;
#Bean
public String mockAddress() {
String mockServerAddress = "http://" + mockServerIP + ":" + mockServerPort;
return mockServerAddress;
}
#Bean
public GetVersionEndPoint getVersionEndPoint() {
return new GetVersionEndPoint();
}
#Bean
public AnalyzeEndPoint analyzeEndPoint() throws JAXBException {
return new AnalyzeEndPoint();
}
#Bean
public PredictionEngineService predictionEngineService() {
return new PredictionEngineService();
}
#Bean
public String studyDetailDemo() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-demo.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
#Bean
public String studyDetailSampleNormal() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-normal.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
#Bean
public String studyDetailSampleCms() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-cms.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
}
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {EndPointTestConfiguration.class,MockServerApp.class
})
#PropertySource(value={"classpath:test.properties"}, ignoreResourceNotFound = true)
public class HttpClientTest {
#Autowired
private String mockAddress;
public static final String request_bad = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|";
public static final String request_good = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
#Test
public void test_bad() throws ClientProtocolException, IOException {
// missing parameter
String result = Request.Post(mockAddress)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_bad, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, false);
String errorMessage = (String) resultJsonObj.get("errorMessage");
assertEquals(errorMessage.contains("Payload has incorrect amount of parts"), true);
}
#Test
public void test_good() throws ClientProtocolException, IOException {
String result = Request.Post(mockAddress)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_good, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), false);
assertEquals(resultJsonObj.containsKey("HasCopay"), true);
assertEquals(resultJsonObj.containsKey("CopayAmount"), true);
assertEquals(resultJsonObj.containsKey("HasCoinsurance"), true);
assertEquals(resultJsonObj.containsKey("CoinsuranceAmount"), true);
assertEquals(resultJsonObj.containsKey("version"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, true);
String version = (String) resultJsonObj.get("version");
assertEquals(version, "0.97");
}
}
You need to move your application.properties to src/test/resource folder. For test, spring load application properties from this path.
You can do as follows :
#RunWith(SpringRunner.class)
#SpringBootTest(classes={Application.class})
public class HttpClientTest {
.............
.................
}
I am newbie to Spring Integration. I am working on solution, but I am stuck on a specific issue while using inbound file adapter ( FileReadingMessageSource ).
I have to read files from different directories and process them and save the files in different directories. As I understand, the directory name is fixed at the start of the flow.
Can some one help me on changing the directory name for different requests.
I attempted the following. First of all, I am not sure whether it is correct way to about and although it worked for only one directory. I think Poller was waiting for more files and never came back to read another directory.
#SpringBootApplication
#EnableIntegration
#IntegrationComponentScan
public class SiSampleFileProcessor {
#Autowired
MyFileProcessor myFileProcessor;
#Value("${si.outdir}")
String outDir;
#Autowired
Environment env;
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext ctx = new SpringApplication(SiSampleFileProcessor.class).run(args);
FileProcessingService gateway = ctx.getBean(FileProcessingService.class);
boolean process = true;
while (process) {
System.out.println("Please enter the input Directory: ");
String inDir = new Scanner(System.in).nextLine();
if ( inDir.isEmpty() || inDir.equals("exit") ) {
process=false;
} else {
System.out.println("Processing... " + inDir);
gateway.processFilesin(inDir);
}
}
ctx.close();
}
#MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
String processFilesin( String inputDir );
}
#Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedDelay(1000).get();
}
#Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
#ServiceActivator(inputChannel = "requestChannel")
#Bean
GenericHandler<String> fileReader() {
return new GenericHandler<String>() {
#Override
public Object handle(String p, Map<String, Object> map) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(p));
Message<File> msg;
while( (msg = fileSource.receive()) != null ) {
fileInChannel().send(msg);
}
return null; // Not sure what to return!
}
};
}
#Bean
public MessageChannel fileInChannel() {
return MessageChannels.queue("fileIn").get();
}
#Bean
public IntegrationFlow fileProcessingFlow() {
return IntegrationFlows.from(fileInChannel())
.handle(myFileProcessor)
.handle(Files.outboundAdapter(new File(outDir)).autoCreateDirectory(true).get())
.get();
}
}
EDIT: Based on Gary's response replaced some methods as
#MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
boolean processFilesin( String inputDir );
}
#ServiceActivator(inputChannel = "requestChannel")
public boolean fileReader(String inDir) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(inDir));
fileSource.afterPropertiesSet();
fileSource.start();
Message<File> msg;
while ((msg = fileSource.receive()) != null) {
fileInChannel().send(msg);
}
fileSource.stop();
System.out.println("Sent all files in directory: " + inDir);
return true;
}
Now it is working as expected.
You can use this code
FileProcessor.java
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
#Component
public class FileProcessor {
private static final String HEADER_FILE_NAME = "file_name";
private static final String MSG = "%s received. Content: %s";
public void process(Message<String> msg) {
String fileName = (String) msg.getHeaders().get(HEADER_FILE_NAME);
String content = msg.getPayload();
//System.out.println(String.format(MSG, fileName, content));
System.out.println(content);
}
}
LastModifiedFileFilter.java
package com.example.demo;
import org.springframework.integration.file.filters.AbstractFileListFilter;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class LastModifiedFileFilter extends AbstractFileListFilter<File> {
private final Map<String, Long> files = new HashMap<>();
private final Object monitor = new Object();
#Override
protected boolean accept(File file) {
synchronized (this.monitor) {
Long previousModifiedTime = files.put(file.getName(), file.lastModified());
return previousModifiedTime == null || previousModifiedTime != file.lastModified();
}
}
}
Main Class= DemoApplication.java
package com.example.demo;
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.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.dsl.core.Pollers;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.integration.file.transformer.FileToStringTransformer;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.stereotype.Component;
#SpringBootApplication
#Configuration
public class DemoApplication {
private static final String DIRECTORY = "E:/usmandata/logs/input/";
public static void main(String[] args) throws IOException, InterruptedException {
SpringApplication.run(DemoApplication.class, args);
}
#Bean
public IntegrationFlow processFileFlow() {
return IntegrationFlows
.from("fileInputChannel")
.transform(fileToStringTransformer())
.handle("fileProcessor", "process").get();
}
#Bean
public MessageChannel fileInputChannel() {
return new DirectChannel();
}
#Bean
#InboundChannelAdapter(value = "fileInputChannel", poller = #Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource() {
CompositeFileListFilter<File> filters =new CompositeFileListFilter<>();
filters.addFilter(new SimplePatternFileListFilter("*.log"));
filters.addFilter(new LastModifiedFileFilter());
FileReadingMessageSource source = new FileReadingMessageSource();
source.setAutoCreateDirectory(true);
source.setDirectory(new File(DIRECTORY));
source.setFilter(filters);
return source;
}
#Bean
public FileToStringTransformer fileToStringTransformer() {
return new FileToStringTransformer();
}
#Bean
public FileProcessor fileProcessor() {
return new FileProcessor();
}
}
The FileReadingMessageSource uses a DirectoryScanner internally; it is normally set up by Spring after the properties are injected. Since you are managing the object outside of Spring, you need to call Spring bean initialization and lifecycle methods afterPropertiesSet() , start() and stop().
Call stop() when the receive returns null.
> return null; // Not sure what to return!
If you return nothing, your calling thread will hang in the gateway waiting for a response. You could change the gateway to return void or, since your gateway is expecting a String, just return some value.
However, your calling code is not looking at the result anyway.
> gateway.processFilesin(inDir);
Also, remove the #Bean from the #ServiceActivator; with that style, the bean type must be MessageHandler.
I am getting PowerMock error but I am not using PowerMocking. I am using normal mocking. This is my class which I want to test:
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
#Component
public class IlinqBeanDataBuilder {
#Autowired
private Settings settings;
private static final String SERVICE_CODE_FOR_FACILITY_COUNTRY = "SERVICE_CODE_FOR_FACILITY_COUNTRY";
private static final String SERVICE_CODE_A_CONSTANT = "A";
private static final String HARD_DOWN = "HARD DOWN";
private static final String QUICK_QUESTION = "QUICK QUESTION";
private static final String PARTIAL_DOWN = "PARTIAL DOWN";
private static final String COMPLETELY_DOWN = "Completely Down";
private static final String UP = "Up";
private static final String PARTIALLY_DOWN = "Partially Down";
public RfsData prepareIlinqData(RequestData requestdata) throws FileNotFoundException {
System.out.println("M into prepare illinq data");
RfsData iLinkDataBean = new RfsData();
iLinkDataBean.setCountryCode(requestdata.getCountryCode());
iLinkDataBean.setSystemId(requestdata.getEquipmentID());
iLinkDataBean.setModality(requestdata.getModality());
/**
* Removing the US from FacilityID In future fix need to change based on
* some related information
*/
iLinkDataBean.setCustomerId(requestdata.getFacilityID().substring(3));
iLinkDataBean.setFirstname(requestdata.getName());
iLinkDataBean.setPnumber(requestdata.getPhoneNumber());
iLinkDataBean.setExtension(requestdata.getExtension());
iLinkDataBean.setDescription(requestdata.getDescription());
iLinkDataBean.setShortDescription(requestdata.getShortDescription());
iLinkDataBean.setPeriod(requestdata.getTimePeriod());
iLinkDataBean.setIlinq_local(requestdata.getLocale());
iLinkDataBean.setRequestApp(requestdata.getRequestingApp());
iLinkDataBean.setExamNum(requestdata.getExamNumber());
iLinkDataBean.setSeriesNum(requestdata.getSeriesNumber());
iLinkDataBean.setImageNum(requestdata.getImageNumber());
iLinkDataBean.setRequestorSystemId(requestdata.getEquipmentID());
iLinkDataBean.setServicecode(requestdata.getServiceCode());
iLinkDataBean.setIsocode(requestdata.getIsoCode());
iLinkDataBean.setProblemtype(requestdata.getProblemType());
if (StringUtils.isNotBlank(requestdata.getEquipmentStatus())
|| StringUtils.isNotEmpty(requestdata.getEquipmentStatus())) {
if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(HARD_DOWN)) {
iLinkDataBean.setStatus(COMPLETELY_DOWN);
} else if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(QUICK_QUESTION)) {
iLinkDataBean.setStatus(UP);
} else if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(PARTIAL_DOWN)) {
iLinkDataBean.setStatus(PARTIALLY_DOWN);
} else {
iLinkDataBean.setStatus(requestdata.getEquipmentStatus());
}
}
String CountryCode = iLinkDataBean.getCountryCode();
System.out.println("Hi country code is:" + CountryCode);
String list1 = this.settings.getString("contactge.americas.country.codes");
System.out.println("list 1 data is:" + list1);
String list2 = this.settings.getString("contactge.asia.country.codes");
String list3 = this.settings.getString("contactge.europe.country.codes");
String list4 = this.settings.getString("contactge.japan.country.codes");
List<String> RegionList1 = Arrays.asList((list1.split(",")));
List<String> RegionList2 = Arrays.asList((list2.split(",")));
List<String> RegionList3 = Arrays.asList((list3.split(",")));
List<String> RegionList4 = Arrays.asList((list4.split(",")));
System.out.println("List1 data is:" + RegionList1);
System.out.println("List2 data is:" + RegionList2);
System.out.println("List3 data is:" + RegionList3);
System.out.println("List4 data is:" + RegionList4);
String RegionCode = null;
if (RegionList1.contains(CountryCode)) {
System.out.println("M in list 1");
RegionCode = "Americas";
} else if (RegionList2.contains(CountryCode)) {
System.out.println("M in list 2");
RegionCode = "ROA";
} else if (RegionList3.contains(CountryCode)) {
System.out.println("M in list 3");
RegionCode = "EMEA";
} else if (RegionList4.contains(CountryCode)) {
System.out.println("M in list 4");
RegionCode = "Japan";
}
System.out.println("Region code:" + RegionCode);
iLinkDataBean.setRegionCode(RegionCode);
System.out.println("ilinq data bean builder data:" + iLinkDataBean);
// Getting regionCode from CountryCode using property file
// iLinkDataBean.setRegionCode(RegionCode);
System.out.println("system id----------------:" + iLinkDataBean.getSystemId());
System.out.println("ilinq data bean builder data:" + iLinkDataBean);
return iLinkDataBean;
}
}
This is my test class. In this, I am mocking setting object which fetch data from database and I am doing inject mock at ILinqBeanDataBuilder.
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import junit.framework.Assert;
#RunWith(MockitoJUnitRunner.class)
public class IlinqBeanDataBuilderTest {
#InjectMocks
IlinqBeanDataBuilder iLinqdata;
#Mock
private Settings settings;
#Before
public void setUp() throws Throwable {
MockitoAnnotations.initMocks(this);
// iLinqdata = new IlinqBeanDataBuilder();
}
#Test
public void testCheckSum() throws FileNotFoundException {
RequestData reqData = new RequestData();
reqData.setCountryCode("123");
reqData.setEquipmentID("414541HD01");
reqData.setModality("sjdksd");
reqData.setFacilityID("sdjhsdj");
reqData.setCountryCode("CA");
reqData.setIsoCode("2");
reqData.setProblemType("ddddddddd");
reqData.setProblemArea("ssbnc");
reqData.setEquipmentStatus("sdsds");
reqData.setName("pinku");
reqData.setPhoneNumber("2999999999992");
reqData.setExamNumber("12323");
reqData.setServiceCode("sdfsfdf");
reqData.setExtension("3473437");
reqData.setDescription("sdsjd");
reqData.setShortDescription("sjdks");
reqData.setTimePeriod("01/01/2011,13:00");
reqData.setLocale("en_US");
reqData.setRequestingApp("icenter");
reqData.setImageNumber("sdjsh");
reqData.setSeriesNumber("2334");
Mockito.when(settings.getString("contactge.americas.country.codes"))
.thenReturn("CA,CL,MX,PR,US,AR,BR,GD,VE,CR,PE,AS,DO,HN,PA,CO,BO,GT,JM,EC,PY,UY,NI,SV");
Mockito.when(settings.getString("contactge.asia.country.codes"))
.thenReturn("IN,CN,AU,HK,KR,MY,SG,TW,TH,ID,PH,NZ,VN,C3,H2,DV");
Mockito.when(settings.getString("contactge.europe.country.codes")).thenReturn(
"AT,BE,CH,DE,ES,FI,FR,GB,IT,NL,PT,SE,UK,NO,DK,PL,RO,ZA,IE,SA,EG,HU,TR,GR,CZ,IL,AE,AL,BG,CS,CY,DZ,IS,JO,KE,KW,LB,LU,MT,MC,MA,RE,RU,SI,SK,TN,UM,AZ,BA,BJ,BY,DI,GE,GF,HR,KZ,LT,LV,MK,UA,YU,OT,NG");
Mockito.when(settings.getString("contactge.japan.country.codes")).thenReturn("JP");
RfsData rfs = iLinqdata.prepareIlinqData(reqData);
System.out.println(rfs.getCountryCode());
Assert.assertEquals("CA", rfs.getCountryCode());
}
}
I am getting this error:
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:54)
at org.mockito.Mockito.mock(Mockito.java:1449)
I guess that Settings is a final class. Mockito 1.x itself cannot mock final classes and probably tries to mock them by including PowerMock - which you don't have installed.
To solve your issue, you can upgrade to at least Mockito 2.1.0 and enable the support for mocking final classes and methods. If you need to stick with an older version, either don't mock final classes, or (if you really have no other choice) give PowerMock a try.