小编Ary*_*rya的帖子

UnexpectedRollbackException:事务已回滚,因为它已标记为仅回滚

我有这种情况:

  1. IncomingMessage表中获取(读取和删除)记录
  2. 阅读记录内容
  3. 在某些表格中插入内容
  4. 如果在步骤1-3中发生错误(任何异常),则将错误记录插入OutgoingMessage
  5. 否则,将成功记录插入OutgoingMessage

所以步骤1,2,3,4应该在一个交易中,或者步骤1,2,3,5

我的流程从这里开始(这是一个计划任务):

public class ReceiveMessagesJob implements ScheduledJob {
// ...
    @Override
    public void run() {
        try {
            processMessageMediator.processNextRegistrationMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
// ...
}
Run Code Online (Sandbox Code Playgroud)

我在ProcessMessageMediator中的主要功能(processNextRegistrationMessage):

public class ProcessMessageMediatorImpl implements ProcessMessageMediator {
// ...
    @Override
    @Transactional
    public void processNextRegistrationMessage() throws ProcessIncomingMessageException {
        String refrenceId = null;
        MessageTypeEnum registrationMessageType = MessageTypeEnum.REGISTRATION;
        try {
            String messageContent = incomingMessageService.fetchNextMessageContent(registrationMessageType);
            if (messageContent == null) {
                return;
            }
            IncomingXmlModel …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate transactions

33
推荐指数
2
解决办法
9万
查看次数

处理MaxUploadSizeExceededException无法停止上传文件

我想检查上传文件的大小并防止文件完全加载到内存中.我正在使用CommonsMultipartFile.上传的文件将被处理并保存在DB中.AbstractCoupleUploadController类处理包含文件的传入请求:

public abstract class AbstractCoupleUploadController<T extends Serializable> extends RemoteServiceServlet implements ServletContextAware,
        UploadServlet<WorkshopHistoryModel>
{
    ...

    @RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView handleRequest(@RequestParam("firstFile") CommonsMultipartFile firstFile,
            @RequestParam("secondFile") CommonsMultipartFile secondFile, HttpServletRequest request, HttpServletResponse response)
    {
        synchronized(this)
        {
            initThreads();
            perThreadRequest.set(request);
            perThreadResponse.set(response);
        }

        handleUpload(firstFile,secondFile,request,response);
        response.getWriter().flush();
        response.flushBuffer();
        return null;
    }

    private void handleUpload(CommonsMultipartFile firstFile, CommonsMultipartFile secondFile, HttpServletRequest request,
        HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html");
        if(firstFile.getSize() == 0 || secondFile.getSize() == 0)
        {
            response.getWriter().print(AppConstants.UPLOAD_ZERO_SIZE_FILE);
            return;
        }

        // other validations
        // uploading:
        try
        {
            String content = request.getParameter(CoupleUploadPanel.CONTENT);
            T model = deserialize(content); …
Run Code Online (Sandbox Code Playgroud)

java spring file-upload spring-mvc apache-commons-fileupload

13
推荐指数
1
解决办法
3071
查看次数

如何在jms-spring集成中将消息选择器注入消息监听器bean?

我正在使用JMS API(使用HornetQ),我正在使用spring bean作为消息监听器容器和消息监听器:

<bean id="messageListener" class="core.messaging.handler.MessageListener">
    <property name="postCommandService" ref="postCommandService" />
</bean>

<bean id="messageSender"
    class="lsn.messaging.sender.MessageSender">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="destination" />
</bean>

<bean id="msgListenerContainer"
  class="org.springframework.jms.listener.DefaultMessageListenerContainer" 
  p:connectionFactory-ref="connectionFactory"
  p:destination-ref="destination"
  p:messageListener-ref="messageListener"
  p:concurrentConsumers="10"
  p:maxConcurrentConsumers="50"
  p:receiveTimeout="5000"
  p:idleTaskExecutionLimit="10"
  p:idleConsumerLimit="5" />
Run Code Online (Sandbox Code Playgroud)

如果我想要我的消息监听器,只消耗特定的消息(具有相同的StringProperty)我该怎么办?我应该在哪里定义选择器?

我有下面的解决方案,但我没有MessageConsumer,所以我不能添加选择器:

     String redSelector = "color='red'";

     MessageConsumer redConsumer = session.createConsumer(queue, redSelector);
     redConsumer.setMessageListener(new SimpleMessageListener("red"));

     TextMessage redMessage = session.createTextMessage("Red");
     redMessage.setStringProperty("color", "red");

     producer.send(redMessage);
Run Code Online (Sandbox Code Playgroud)

messaging spring jms

8
推荐指数
1
解决办法
1万
查看次数

向“方法不应有太多参数”SonarQube 规则添加自定义例外

在声纳规则中,有一个S00107规则“方法不应有太多参数”。此规则对某些注释有例外:

例外

使用Spring的@RequestMapping(以及相关的快捷方式注解,如@GetRequest)或@JsonCreator注解的方法可能有很多参数,可以封装。因此,此类方法被忽略。

是否可以为此异常添加另一个注释?

我的情况:我有构造函数,由 Lombok 注释@Builder,其中包含很多参数!所以我想忽略这些构造函数的这条规则。

public class MyClass extends MySupperClass {
  @Builder
  public MyClass(String a, int b, ..., String z) {
  }
}
Run Code Online (Sandbox Code Playgroud)

java lombok sonarqube

8
推荐指数
1
解决办法
1113
查看次数

GWT RadioButton Change Handler

我有一个带有RadioButton选项和标签投票的投票小部件

  1. 当用户选择一个选项时,选择投票应该+1;
  2. 当选择另一个选择时,旧选择投票应为-1,新选择投票应为+1.

我为此使用了ValueChangeHandler:

valueRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> e) {
                if(e.getValue() == true)
                {
                    System.out.println("select");
                    votesPlusDelta(votesLabel, +1);
                }
                else
                {
                    System.out.println("deselect");
                    votesPlusDelta(votesLabel, -1);
                }
            }
        }); 

private void votesPlusDelta(Label votesLabel, int delta)
{
    int votes = Integer.parseInt(votesLabel.getText());
    votes = votes + delta;
    votesLabel.setText(votes+"");
}
Run Code Online (Sandbox Code Playgroud)

当用户选择新选项时,较旧的选择侦听器应该跳入else语句,但不会(仅+1部分工作).我该怎么办?

java gwt listener radio-button

6
推荐指数
1
解决办法
9967
查看次数

从Jasperreports中的服务器接收多个不同的Content-Disposition标头

我正在尝试在servlet的响应中设置content-disposition头,但我在浏览器中收到此错误.我该怎么办?

从服务器收到重复的标头

来自服务器的响应包含重复的标头.此问题通常是配置错误的网站或代理的结果.只有网站或代理管理员才能解决此问题.

错误349(net :: ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION):收到多个不同的Content-Disposition标头.不允许这样做以防止HTTP响应分裂攻击.

这是我的servlet控制器:

@RequestMapping("/**/paymentOrderReport.pdf")
public class PaymentOrderReportViewController extends org.springframework.web.servlet.mvc.AbstractController {

    private PaymentDao paymentDao;
    private JasperPdfView pdfView;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment; filename=" + "report.pdf");

        PaymentOrderEntity paymentOrderEntity = null;
        String traceCode = request.getParameter(ParamConstants.TRACE_CODE);

        if (traceCode != null) {
            PaymentSheetRequestEntity payRequestEntity = paymentDao.loadByUniqueProperty(PaymentSheetRequestEntity.PROP_TRACE_CODE,
                    traceCode);
            if (payRequestEntity != null) {
                paymentOrderEntity = payRequestEntity.getPaymentOrder();
            }
        }

        if (paymentOrderEntity != null) {
            List<PaymentOrderEntity> result = new ArrayList<PaymentOrderEntity>();
            result.add(paymentOrderEntity);
            JRDataSource jrDataSource = new JRBeanCollectionDataSource(result); …
Run Code Online (Sandbox Code Playgroud)

java servlets jasper-reports http-headers

6
推荐指数
1
解决办法
1万
查看次数

使用条件计算hibernate中按行分组的数量

我想group by用hibernate Criteria API计算行数,但我只计算每个组中聚合的行数:

ProjectionList projectionList = Projections.projectionList()
        .add(Projections.groupProperty("color"))
        .add(Projections.rowCount());
Criteria criteria = session.createCriteria("ProductEntity");
criteria.setProjection(projectionList);
// adding some criteria
List results = criteria.list();
Run Code Online (Sandbox Code Playgroud)

上面的代码将导致此查询:

select p.color, count(*) from product p group by p.color
Run Code Online (Sandbox Code Playgroud)

但我想要这个查询:

select count(*) from (select p.color from product p group by p.color)
Run Code Online (Sandbox Code Playgroud)

我知道HQL可能,但我不想使用它.那么我如何使用Criteria API执行此操作?

java sql hibernate group-by

6
推荐指数
1
解决办法
6254
查看次数

sessionTransacted和JmsTransactionManager之间的区别

使用sessionTransacted = true(在JmsTemplate和/或DefaultMessageListenerContainer中)与使用JmsTransactionManager之间的主要区别是什么?对于JmsTemplate和DefaultMessageListenerContainer用法,使用sessionTransacted = true是否足够?(我不需要XA)

该文档说(在JmsAccessor的setSessionTransacted方法中),看来应该不成问题:

将此标志设置为“ true”将在托管事务之外运行时使用简短的本地JMS事务,如果存在托管事务(XA事务除外),则将使用同步的本地JMS事务。

java spring jms spring-jms

5
推荐指数
1
解决办法
7199
查看次数

如果没有做客户确认会怎么样?

我有:

  • 一个hornetq-2.2.14-final独立服务器
  • 客户端应用程序C1,它将消息发送到队列A.
  • 消耗来自队列A的消息的客户端应用程序C2

C1使用jmstemplate在CLIENT_ACKNOWLEDGE模式下发送消息:

<bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE" />
    <property name="sessionTransacted" value="true" />
</bean>
Run Code Online (Sandbox Code Playgroud)

所以C2应该手动确认消息:

@Override
@Transactional
public void onMessage(Message message) 
{
    try 
    {
        messageHandlerService.handleReceivedMessage(message);
        message.acknowledge();
    } 
    catch (DeserializeXmlException e) 
    {
        // TODO log
        e.printStackTrace();
    }   
    catch (InvalidMessageException e) 
    {
        //TODO log
        e.printStackTrace();
    }
    catch (JMSException e) 
    {
        //TODO log
        e.printStackTrace();
    }   
}
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 当客户端C2收到该消息但在确认之前崩溃时,消息会发生什么?
  • 有没有超时机制?如果是,确认的默认超时是多少?我怎么修改它?

java jms hornetq

3
推荐指数
1
解决办法
1694
查看次数

与 hibernate 组中的其他字段一起计数

我想通过休眠返回组中其他字段的行,并且我的实体类中没有任何字段表示计数。例如我有一个 Payment 实体类:

class Payment
{
    private Long id;
    private String totalCode;
    private String activityCode;
    private Long amount;

    // other fields and setter/getters
}
Run Code Online (Sandbox Code Playgroud)

sql查询:

select count(*), sum(p.amount), p.total_code, p.activity_code 
from  tb_payment p
group by p.total_code,p.activity_code
Run Code Online (Sandbox Code Playgroud)

和我的休眠标准:

Session session = getCurrentSession();
ProjectionList projectionList = Projections.projectionList();        
projectionList.add(Projections.groupProperty("totalCode"))
        .add(Projections.groupProperty("activityCode"))
        .add(Projections.sum("amount"))
        .add(Projections.count("id"));
Criteria criteria  = session.createCriteria(Payment.class);
criteria.setProjection(projectionList);
List<Payment> payments = criteria.list();
Run Code Online (Sandbox Code Playgroud)

正如我所说,我的问题是我不知道在哪里/如何访问计数的值(来自criteria.list())!?

java sql hibernate group-by criteria

3
推荐指数
1
解决办法
7762
查看次数

MongoDB中的高可用性

大家都说mongoDB是CAP定理中的CP!但是使用主从复制时,它也具有高可用性(如果主要失败,其余成员将自动尝试选择新的主要).我的问题是,在哪些情况下(以及如何)它可以拥有AP(具有最终一致性)?

document-oriented-db mongodb

2
推荐指数
1
解决办法
2443
查看次数

在 Akka 中识别和处理闲置演员的最佳实践

我是 Akka 框架的新手,我正在用它构建一个群聊应用程序。我的应用程序可能有 1000 万个相同类型的 actor 实例(每个群聊一个 actor 实例),其中只有 5% 是高度活跃的,而其中 60% 可以闲置数天(不接收任何消息)。

我想知道:

  1. 是否有任何最佳实践来识别这些闲置演员?
  2. 处理它们的最佳做法是什么?阻止他们就够了吗?

actor akka

2
推荐指数
1
解决办法
1177
查看次数