我需要编写一个应用程序,我可以使用spring-data和mongodb进行复杂的查询.我一直在使用MongoRepository开始,但在复杂的查询中苦苦寻找示例或实际理解语法.
我在谈论这样的查询:
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
List<User> findByEmailOrLastName(String email, String lastName);
}
Run Code Online (Sandbox Code Playgroud)
或者使用基于JSON的查询,我尝试通过反复试验,因为我没有正确的语法.即使在阅读了mongodb文档(由于语法错误而导致的非工作示例).
@Repository
public interface UserRepositoryInterface extends MongoRepository<User, String> {
@Query("'$or':[{'firstName':{'$regex':?0,'$options':'i'}},{'lastName':{'$regex':?0,'$options':'i'}}]")
List<User> findByEmailOrFirstnameOrLastnameLike(String searchText);
}
Run Code Online (Sandbox Code Playgroud)
阅读完所有文档后,似乎mongoTemplate
记录得更好MongoRepository
.我指的是以下文档:
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/
你能告诉我什么更方便,更强大吗?mongoTemplate
还是MongoRepository
?两者是否同样成熟,或者其中一个缺少比另一个更多的功能?
在Java中:
Lock lock = new ReentrantLock();
try{
lock.lock();
someFunctionLikelyToCauseAnException();
}
catch(e){...}
finally {
lock.unlock();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是上面这个例子我们知道锁总是会被解锁,因为最终总是执行,但是C++的保证是什么?
mutex m;
m.lock();
someFunctionLikelyToCauseAnException();
/// ????
Run Code Online (Sandbox Code Playgroud)
这将如何工作?为什么?
所以我正在使用这个小的Angular + Java + Spring Boot + MongoDB应用程序.它最近得到了很多动作(阅读:代码修改),但数据访问类基本上没有被触及的AFAIK.
然而,似乎MongoRepository
突然决定停止持续改变我save()
对DB 的更改.
检查mongod.log
这是我在save()
工作时看到的:
2018-04-11T15:04:06.840+0200 I COMMAND [conn6] command pdfviewer.bookData command: find { find: "bookData", filter: { _id: "ID_1" }, limit: 1, singleBatch: true } planSummary: IDHACK keysExamined:1 docsExamined:1 idhack:1 cursorExhausted:1 keyUpdates:0 writeConflicts:0 numYields:1 nreturned:1 reslen:716 locks:{ Global: { acquireCount: { r: 4 } }, Database: { acquireCount: { r: 2 } }, Collection: { acquireCount: { r: 2 } } } protocol:op_query 102ms
2018-04-11T17:30:19.615+0200 …
Run Code Online (Sandbox Code Playgroud) 我记得我们不能杀死当前正在运行的Quartz Job,但是我们可以中断并在必要时进行布尔检查,无论我们是否需要继续进行后续操作.
即使我们实现InterruptableJob并调用scheduler.interrupt来中断Job,当前执行的作业仍将在服务器中运行.
例如:
http://neopatel.blogspot.in/2011/05/quartz-stop-job.html http://forums.terracotta.org/forums/posts/list/3191.page
有人可以纠正我的理解并解释我如何杀死或阻止"当前"执行的工作?
谢谢,凯西尔
正如SOLID原则所说,最好通过将切换条件转换为类和接口来删除它们.我想用这段代码做:
注意:这段代码不是真正的代码,我只想把它放进去.
MessageModel message = getMessageFromAnAPI();
manageMessage(message);
...
void manageMessage(MessageModel message){
switch(message.typeId) {
case 1: justSave(message); break;
case 2: notifyAll(message); break;
case 3: notify(message); break;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想删除switch语句.所以我为它创建了一些类,我尝试在这里实现一个多态:
interface Message{
void manageMessage(MessageModel message);
}
class StorableMessage implements Message{
@Override
public void manageMessage(MessageModel message) {
justSave(message);
}
}
class PublicMessage implements Message{
@Override
public void manageMessage(MessageModel message) {
notifyAll(message);
}
}
class PrivateMessage implements Message{
@Override
public void manageMessage(MessageModel message) {
notify(message);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我调用我的API来获取我的MessageModel
:
MessageModel message = getMessageFromAnAPI(); …
Run Code Online (Sandbox Code Playgroud) 我正在为我的游戏开发网络,并且运行客户端和服务器.服务器当前在另一个服务器中无限循环Thread
,但我的客户端和游戏代码在同一个线程中.当客户端处理来自服务器的数据时,我遇到了问题,游戏一直挂起,直到客户端完成处理新数据包.
我试图解决这个问题,通过使客户端类实现Runnable
,并在一个单独的线程中运行.我遇到了一些其他错误,我想知道这是不是问题.
我有run
方法和sendPacket
方法:
public void run() {
// empty
}
public void sendPacket() {
somePacketSendingCode();
}
Run Code Online (Sandbox Code Playgroud)
run
方法中没有代码,因为我只使用该sendPacket
方法.sendPacket
接收数据包时由侦听器线程调用.
如果我在run
方法中没有代码,这是否意味着客户端Thread
在启动后停止执行?如果是这样,那是不是意味着该sendPacket
方法什么都不做?
几天前,我得到了一张支持票NullPointerException
:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.redacted.SalesResponsePagination com.redacted.StatisticsService.findSalesData(com.redacted.ConfStats) throws com.redacted.AsyncException' threw an unexpected exception: java.lang.NullPointerException
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at ... (typical GWT + Tomcat stacktrace)
Caused by: java.lang.NullPointerException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.util.concurrent.ForkJoinTask.getThrowableException(Unknown Source)
at java.util.concurrent.ForkJoinTask.reportException(Unknown Source)
at java.util.concurrent.ForkJoinTask.invoke(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at com.redacted.StatisticsControllerImpl.replacePrices(StatisticsControllerImpl.java:310)
at com.redacted.StatisticsControllerImpl.findSalesData(StatisticsControllerImpl.java:288)
at com.redacted.StatisticsServiceImpl.findSalesData(StatisticsServiceImpl.java:83)
at sun.reflect.GeneratedMethodAccessor752.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Kotlin,并在kotlin中创建了一个简单的类,在其构造函数中使用两个int,然后添加它们.这些int可以为null.
I am receiving compilation error as:
None of the following functions can be called with the arguments supplied:
public final operator fun plus(other: Byte): Int defined in kotlin.Int
public final operator fun plus(other: Double): Double defined in kotlin.Int
public final operator fun plus(other: Float): Float defined in kotlin.Int
public final operator fun plus(other: Int): Int defined in kotlin.Int
public final operator fun plus(other: Long): Long defined in kotlin.Int
public final operator fun plus(other: Short): Int defined in kotlin.Int
Run Code Online (Sandbox Code Playgroud)
我的号码加法器类如下: …
我在Quartz.NET文档的这个教程部分看到,应该可以定义Quartz调度程序将使用的最大线程数.在我的特殊情况下,我想将此数字设置为1.但是在API文档中,我找不到一种方法来访问我的调度程序正在使用的线程池实例并在其上设置任何属性.
目前我的代码如下所示:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler scheduler = schedFact.GetScheduler();
scheduler.Start();
// Setup jobs and triggers and then call scheduler.ScheduleJob...
Run Code Online (Sandbox Code Playgroud)
有人知道如何设置池中的线程数吗?
提前感谢您的帮助!
查看Java的String
类,我们可以看到哈希代码在第一次评估后被缓存.
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
Run Code Online (Sandbox Code Playgroud)
hash
实例变量在哪里.我有一个问题,为什么我们需要h
额外的变量?
java ×6
mongodb ×2
spring-data ×2
.net ×1
c++ ×1
hashcode ×1
int ×1
jit ×1
kotlin ×1
lambda ×1
locking ×1
quartz.net ×1
reflection ×1
spring ×1
spring-boot ×1
stack-trace ×1