Mirth Connect 3.7.0队列监控邮件脚本触发NumberFormatException错误求助
问题分析与解决方案
核心错误定位
从错误栈可以明确:NumberFormatException: null 是在调用smtpConn.send()时触发的,根源是Mirth内部尝试将全局SMTP配置中的端口值转换为整数时遇到了null,对应代码路径为ServerSMTPConnection.send() → Integer.parseInt()。
你使用的SMTPConnectionFactory.createSMTPConnection()会直接读取Mirth Connect全局设置的SMTP参数,因此优先排查全局SMTP配置的完整性。
分步排查与修复
1. 检查Mirth全局SMTP配置
- 登录Mirth Connect管理控制台,进入Settings > Server Settings > SMTP
- 确认以下配置项完整有效:
- SMTP Server地址不能为空
- Port必须是合法数字(如默认25、465或587,注意SSL/TLS对应端口差异:465对应SSL,587对应STARTTLS)
- 验证SMTP账号、密码、发件人地址等信息是否正确
- 点击页面的Test Connection按钮,确认连接可正常建立
2. 修复脚本空值处理逻辑
首次执行脚本时,globalMap.get(channelId + '_lastalert')会返回null,导致datediff计算结果为NaN,进而触发不必要的告警分支。修改脚本中的日期差计算逻辑:
var rightnow = new Date(); var hLimit = 23; var lastsent = globalMap.get(channelId + '_lastalert'); var datediff = hLimit + 1; // 默认允许发送告警 // 仅当存在上次告警记录时计算时间差 if (lastsent !== null) { datediff = (rightnow - lastsent) / (1000 * 60 * 60); } if (datediff < hLimit) { return; } else { var channelName = ChannelUtil.getChannelName(channelId); var mQueued = ChannelUtil.getQueuedCount(channelId); var qAlarm = 100; if (mQueued >= qAlarm) { var emTo = 'none@email.com'; var emCC = 'none@email.com'; var emFrom = 'none@email.com'; var emSubj = 'Queue Alert for Channel (TEST): ' + channelName; var emBody = 'Queued messages detected on channel (TEST) ' + channelName + '.\n'; emBody += 'Current number of queued messages is ' + mQueued + '.\n'; emBody += 'This alert will not be resent for ' + hLimit + ' hours.\nPlease do not respond to this email. This address is not monitored.\n'; emBody += 'Message Sent at ' + rightnow + ' \n'; var smtpConn = SMTPConnectionFactory.createSMTPConnection(); smtpConn.send(emTo,emCC,emFrom,emSubj,emBody); globalMap.put(channelId + '_lastalert',rightnow); } }
3. 优化脚本执行逻辑
Postprocessor是针对每条消息触发执行,频繁触发会占用系统资源,建议将队列监控逻辑迁移到定时任务中:
- 在Mirth控制台创建定时任务(如每5分钟执行一次),复用现有队列统计与告警代码
- 避免单条消息处理时重复执行监控逻辑,提升系统性能
内容的提问来源于stack exchange,提问作者de2425
相关产品推荐
相关产品推荐

