gpt4 book ai didi

java - 如何使 Camel 路线线程安全?

转载 作者:行者123 更新时间:2023-12-02 04:01:47 27 4
gpt4 key购买 nike

我有一个 Camel 路由,它消耗来自 apache activeMQ 的任务。当 ActiveMQ 只有一个使用者时,一切工作正常。

但是当消费者增加到两个(或更多)时,应用程序的行为就会变得不恰当。

这是我的路线:

<routeContext id="myRoute"  xmlns="http://camel.apache.org/schema/spring">
<route errorHandlerRef="myErrorHandler" id="myErrorRoute">
<from uri="activemq:queue:{{my.queue}}" />
<log loggingLevel="DEBUG" message="Message received from my queue : ${body}"></log>
<multicast>
<pipeline>
<log loggingLevel="DEBUG" message="Adding to redis : ${body}"></log>
<to uri="spring-redis://localhost:6379?serializer=#stringSerializer" />
</pipeline>
<pipeline>
<transform>
<method ref="insertBean" method="myBatchInsertion"></method>
</transform>
<choice>
<when>
<simple> ${body.size()} == ${properties:my.batch.size}</simple>
<log message="Going to insert my batch in database" />
<to uri="mybatis:batchInsert?statementType=InsertList"></to>
<log message="Inserted in my table : ${in.header.CamelMyBatisResult}"></log>
<choice>
<when>
<simple>${properties:my.write.file} == true</simple>
<bean beanType="com.***.***.processors.InsertToFile"
method="processMy(${exchange}" />
<log message="Going to write to file : ${in.header.CamelFileName}" />
<to uri="file://?fileExist=Append&amp;bufferSize=32768"></to>
</when>
</choice>
</when>
</choice>
</pipeline>
</multicast>
</route>
</routeContext>

下面是 bean :

public class InsertBeanImpl {
public List<Out> myOutList = new CopyOnWriteArrayList<Out>();

public List<Out> myBatchInsertion(Exchange exchange) {
if (myOutList.size() >= myBatchSize) {
Logger.sysLog(LogValues.info,this.getClass().getName(),"Reached max PayLoad size : "+myOutList.size() + " , going to clear batch");
myOutList.clear();
}
Out myOut = exchange.getIn().getBody(Out.class);
Logger.sysLog(LogValues.APP_INFO, this.getClass().getName(), myOut.getMasterId()+" | "+"Adding to batch masterId : "+myOut.getMasterId());
synchronized(myOut){
myOutList.add(myOut);
}
Logger.sysLog(LogValues.info, this.getClass().getName(), "Count of batch : "+myOutList.size());
return myOutList;
}
}



public class SaveToFile {
static String currentFileName = null;
static int iSub = 0;
String path;
String absolutePath;

@Autowired
private Utility utility;


public void processMy(Exchange exchange) {
getFileName(exchange, currentFileNameSub, iSub);
}


public void getFileName(Exchange exchange, String outFile, int i) {
exchange.getIn().setBody(getFromJson(exchange));
path = (String) exchange.getIn().getHeader("path");
Calendar date = null;
date = new GregorianCalendar();
NumberFormat format = NumberFormat.getIntegerInstance();
format.setMinimumIntegerDigits(2);
String pathSuffix = "/" + date.get(Calendar.YEAR) + "/"
+ format.format((date.get(Calendar.MONTH) + 1)) + "/"
+ format.format(date.get(Calendar.DAY_OF_MONTH));
String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
double megabytes = 100 * 1024 * 1024;
if (outFile != null) {
if (!fileName.equals(outFile.split("_")[0])) {
outFile = null;
i = 0;
}
}
if (outFile == null) {
outFile = fileName + "_" + i;
}
while (new File(path + "/" + pathSuffix + "/" + outFile).length() >= megabytes) {
outFile = fileName + "_" + (++i);
}
absolutePath = path + "/" + pathSuffix + "/" + outFile;
exchange.getIn().setHeader("CamelFileName", absolutePath);
}

public String getFromJson(Exchange exchange) {
synchronized(exchange){
List<Out> body = exchange.getIn().getBody(CopyOnWriteArrayList.class);
Logger.sysLog(LogValues.info, this.getClass().getName(), "body > "+body.size());
String text = "";
for (int i = 0; i < body.size(); i++) {
Out msg = body.get(i);
text = text.concat(utility.convertObjectToJsonStr(msg) + "\n");
}
return text;
}

}
}

由于处理器不同步且线程不安全,因此在有多个使用者的情况下,路由无法按预期工作。

有人能告诉我如何使我的路线线程安全或同步吗?

我尝试使处理器同步,但没有帮助。还有其他方法可以做到这一点吗?

最佳答案

确保您的处理器线程安全,并且不要使用太多 Synchronize,或者最好根本不使用 Synchronize。

要做到这一点,其中根本不能有可更改的实例变量。

只有通用属性可以存在,例如某些对所有线程有效且在任何方法执行期间都不会更改的设置。这样就不需要使用影响性能的Synchronize机制了。

Camel 中的其他所有内容都是线程安全的,如果处理器实现不安全,则无法告诉 Camel 线程安全。

关于java - 如何使 Camel 路线线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41569765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com