There is a feature on the CouchbaseTemplate class in Spring Data Couchbase to set how exceptions are handled on writes, using the setWriteResultChecking method.
There doesn't seem to be any information on this in the documentation.
If you dig into the source code, the default setting is NONE. This means that exceptions get logged but don't get passed back up to your calling code.
Please does anyone know why this is the default behaviour? Wouldn't it make more sense to use EXCEPTION, so at least your calling code knows that something has gone wrong?
I've not got a lot of experience with Spring data for Couchbase, or indeed Couchbase, so I feel like I'm missing a fundamental point here.
Thanks.
For future reference, this is how I set the write checking to EXCEPTION:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.WriteResultChecking;
#Configuration
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {
#Override
#Bean(name = BeanNames.COUCHBASE_TEMPLATE)
public CouchbaseTemplate couchbaseTemplate() throws Exception {
CouchbaseTemplate couchbaseTemplate = super.couchbaseTemplate();
couchbaseTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
return couchbaseTemplate;
}
}
I didn't include this as an answer, because it isn't answering the question. The quesstion was about why NONE is the default.
Related
Small question regarding a Java and Spotbugs issue please.
I have a very simple class:
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class Question {
private final MeterRegistry meterRegistry;
#Autowired
public Question(final MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
public void foo() {
Counter.builder("some-name").register(meterRegistry).increment();
}
}
And as you see in the class, I have an object MeterRegistry meterRegistry.
This object comes from io.micrometer.core.instrument package, I have no control over it.
100% reproducible, I am getting this from Findbugs/Spotbugs:
Question(MeterRegistry) may expose internal representation by storing an externally mutable object into Question.meterRegistry
May I ask how can I fix tis please?
I tried looking up the API for a .clone() method or something equivalent, no luck.
I am quite interested into solving this.
Thank you
I want to test my scheduled task, so I followed this tutorial
#SpringJUnitConfig(SchedulerConfig.class)
public class MailJobFinderTaskIT {
#SpyBean
private MailJobFinderTask mailJobFinderTask;
#Test
public void whenWaitThreeSecond_ThenTaskCalledThreeTimes(){
await()
.atMost(Duration.ofSeconds(3))
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs());
}
}
But actually it does not work, because the the following error
org.mockito.exceptions.misusing.NullInsteadOfMockException:
Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
not: verify(mock.someMethod());
Also, if you use #Mock annotation don't miss initMocks()
here is the signature of my Task-Class
#Component
public class MailJobFinderTask extends SuppressedLogPoller {
....
}
#Scheduled(fixedRate = 1000)
public void findEmailJobs() {
.
.
}
I tried already to change the Annotation to #SpringBootTest and also tried to used #MockBean instead of #SpyBean but without any success. Actually I do not understand why my bean mailJobFinderTask is not created
Your code seems to work fine on my system, but I had to do a few changes.
I would recommend these as starting points to your troubleshooting. Also, you haven't mentioned if you can successfully replicate the linked tutorial, which also runs fine on my system. If you haven't, I would recommend trying the tutorial first, confirming it works, and then adding your own code.
If the below does not help you, then it could be that the issue is not in the part of the code you posted. In that case, sharing the full code would help us better troubleshoot the problem. But for now, maybe pay particular attention to the import statements and the main changes listed below.
Changes from your code
I changed the Duration.ofSeconds(3) method to Duration.FIVE_SECONDS. The reason for this is that the former is from java.time package, while await() expects a Duration class from the awaitility package. I just used FIVE_SECONDS for compatibility purposes.
I had to remove the "extends SuppressedLogPoller" part, as I did not have access to this class. I would also recommend removing it and adding this back again to see if it is causing the issue.
Test class code
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
#SpringJUnitConfig(ScheduledConfig.class)
public class MailJobFinderTaskIT {
#SpyBean
private MailJobFinderTask mailJobFinderTask;
#Test
public void whenWaitThreeSecond_ThenTaskCalledThreeTimes(){
await()
.atMost(Duration.FIVE_SECONDS)
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(7)).findEmailJobs());
}
}
Domain class
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
#Component
public class MailJobFinderTask {
#Scheduled(fixedRate = 1200)
public void findEmailJobs() {
}
}
As mentioned in the error message:
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
not: verify(mock.someMethod());
You should update the following line:
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs());
as shown below:
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs();
I'm trying to make use of the Optaplanner constraintprovider, which works fine until I want to use the count() ConstraintCollector.
I try to use it in a groupBy-clause, but I get the error: The method count() is undefined for the type hamxConstraintProvider
I was under the assumption this should "just work"? Or should I write my own method for count? I couldn't find that happening in the examples, but seem unable to fix it either. Am I overlooking an import?
import org.optaplanner.core.api.score.stream.Constraint;
import org.optaplanner.core.api.score.stream.ConstraintFactory;
import org.optaplanner.core.api.score.stream.ConstraintProvider;
import org.optaplanner.core.api.score.stream.uni.UniConstraintCollector;
import org.optaplanner.core.api.score.stream.bi.BiConstraintCollector;
import org.optaplanner.core.api.score.stream.ConstraintCollectors.*;
import org.optaplanner.core.api.score.stream.Joiners.*;
...
public class hamxConstraintProvider implements ConstraintProvider{
#Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[] {
skillUnavailable(constraintFactory),
balancedJobs(constraintFactory)
};
}
...
private Constraint balancedJobs(ConstraintFactory factory) {
return factory.from(Job.class)
.groupBy(Job::getEmployee,count())
.penalize("unbalancedEmployeeUsage", HardSoftScore.ONE_SOFT,count);
}
Instead of
import org.optaplanner.core.api.score.stream.ConstraintCollectors.*;
import org.optaplanner.core.api.score.stream.Joiners.*;
use static imports:
import static org.optaplanner.core.api.score.stream.ConstraintCollectors.*;
import static org.optaplanner.core.api.score.stream.Joiners.*;
The former imports classes, the latter imports static methods.
count() and countLong() are methods of ConstraintCollectors. If it compiles but doesn't run, you're likely not providing Spring's ClassLoader to the SolverConfig.
In the next release, 7.32, we're releasing optaplanner-spring-boot-starter, which will make Spring Boot integration a lot easier (it just auto injects a SolverManager (or SolverFactory, but the former is the latter on steriods)
A null pointer exception occurred when trying to connect to hybris database using hybris flexible search service seemingly due to getJaloResult() method.
I need to retrieve certain data from hybris commerce database. I tried to use hybris flexible service to do that by using defaultFlexibleSearchService.search() method, but I got a null pointer exception. It seems that the exception occurred when search() method tries to call getJaloResult() method. I have no clue about the solution - thanks for any hints.
My class definition code is here
package de.hybris.platform.integrationservices.audit;
import java.util.stream.Stream;
import com.sun.tools.javac.util.List;
import de.hybris.platform.audit.TypeAuditReportConfig;
import de.hybris.platform.audit.view.AuditViewService;
import de.hybris.platform.audit.view.impl.ReportView;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.RelationQuery;
import de.hybris.platform.servicelayer.search.SearchResult;
import de.hybris.platform.servicelayer.search.impl.DefaultFlexibleSearchService;
import de.hybris.platform.integrationservices.model.IntegrationObjectModel;
public class IntegrationObjectAudit implements AuditViewService
{
private DefaultFlexibleSearchService defaultFlexibleSearchService;
public IntegrationObjectAudit() {
defaultFlexibleSearchService = new DefaultFlexibleSearchService();
}
public List<IntegrationObjectModel> searchModel(){
String query = "SELECT {PK} FROM {IntegrationObject}";
FlexibleSearchQuery flexibleSearchQuery = new FlexibleSearchQuery(query);
flexibleSearchQuery.setCount(1);
de.hybris.platform.servicelayer.search.SearchResult<IntegrationObjectModel> resListIntegrationModel = this.defaultFlexibleSearchService.search(query);
List<IntegrationObjectModel> resList = (List<IntegrationObjectModel>) resListIntegrationModel.getResult();
return resList;
}
}
Didnt notice it was posted days back. Hope you already resolved it. Still adding my answer as it may be helpful for others.
What I see you are not injecting flexibleSearchService bean in IntegrationObjectAudit. And as flexibleSearchService bean is not injected, its causing NullPointerException when you call any function on it.
You can fix it as following (spring bean injection concept)
Either you should create a setter function and inject it by spring xml
Or use #Resource or #Autowire annotation for same
public class IntegrationObjectAudit implements AuditViewService
{
#Resource
private DefaultFlexibleSearchService flexibleSearchService;
.....
Hope it helps. Let me know please.
I'm checking out the Play! Framework, using Java (don't want to learn a new framework and a new language at the same time - I'll incorporate Scala as I learn that), and so far it's awesome.
I'm having a bit of difficulty with forms though. I'm still stuck on the first part here and, as far as I understand, I somehow need to get an instance of FormFactory or something related, however I have no idea in which package it might be located, or whether formFactory is also another magic method (like ok).
Any pointers would be appreciated!
EDIT Here's my code:
package controllers;
import com.google.inject.Inject;
import play.data.FormFactory;
import play.api.data.Form;
import play.mvc.*;
public class User extends Controller {
#Inject
FormFactory form;
final static Form<model.User> userForm = form(model.User.class);
public Result post() {
model.User user = userForm.bindFromRequest().get();
return ok("The form was received!: " + user);
}
}
The play.data package doesn't exit for me. Maybe I did an incorrect install? To be clear, I did start this project from IntelliJ
One issue is that your form should not be static as there is no way to initialize it before FormFactory is injected.
According to docs this call
Form<model.User> userForm = form(model.User.class);
should be
Form<model.User> userForm = form.form(model.User.class)
as form is method of FormFactory.
If the package is missing from class path its some configuration issue or wrong play version. A working project can be usually obtained through activator.