System environment
System: window 10
JDK: java version “11.0.21”
ROCKETMQ: rocketmq-all-5.1.4-bin-release
Installation steps
- rocketMq download address official website: http://rocketmq.apache.org/dowloading/releases/
- Unzip the configuration environment variables, manually add the environment variable named ROCKETMQ_HOME, and index to the RocketMQ decompression directory
3.JDK11, you need to modify some configurations to start RocketMQ, modify %ROCKETMQ_HOME%/bin/runserver.cmd
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & amp; EXIT /B 1 set "JAVA=%JAVA_HOME%\bin\java.exe" setlocal set BASE_DIR=%~dp0 set BASE_DIR=?SE_DIR:~0,-1% for %%d in (?SE_DIR%) do set BASE_DIR=%%~dpd set CLASSPATH=.;?SE_DIR%conf;?SE_DIR%lib\*;%CLASSPATH% set "JAVA_OPT=%JAVA_OPT% -server -Xms256m -Xmx512 -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m" set "JAVA_OPT=%JAVA_OPT% -XX: + UseConcMarkSweepGC -XX: + UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX: + CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX: + CMSClassUnloadingEnabled -XX:SurvivorRatio=8 -XX:- UseParNewGC" set "JAVA_OPT=%JAVA_OPT% -verbose:gc -Xloggc:"%USERPROFILE%\rmq_srv_gc.log" -XX: + PrintGCDetails -XX: + PrintGCDateStamps" set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow" set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages" set "JAVA_OPT=%JAVA_OPT% %JAVA_OPT_EXT% -cp "%CLASSPATH%"" "%JAVA%" %JAVA_OPT% %*
Modify %ROCKETMQ_HOME%/bin/runbroker.cmd
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & amp; EXIT /B 1 set "JAVA=%JAVA_HOME%\bin\java.exe" setlocal set BASE_DIR=%~dp0 set BASE_DIR=?SE_DIR:~0,-1% for %%d in (?SE_DIR%) do set BASE_DIR=%%~dpd set CLASSPATH=.;?SE_DIR%conf;?SE_DIR%lib\*;%CLASSPATH% rem ================================================= ========================================== rem JVM Configuration rem ================================================= ========================================== set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g" set "JAVA_OPT=%JAVA_OPT% -XX: + UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8" set "JAVA_OPT=%JAVA_OPT% -verbose:gc -Xlog:gc*:file=%USERPROFILE%/mq_gc.log:time,tags:filecount=5,filesize=30M" set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow" set "JAVA_OPT=%JAVA_OPT% -XX: + AlwaysPreTouch" set "JAVA_OPT=%JAVA_OPT% -XX:MaxDirectMemorySize=15g" set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages -XX:-UseBiasedLocking" set "JAVA_OPT=%JAVA_OPT% -Drocketmq.client.logUseSlf4j=true" set "JAVA_OPT=%JAVA_OPT% %JAVA_OPT_EXT% -cp "%CLASSPATH%"" "%JAVA%" %JAVA_OPT% %*
- Start RocketMq
start mqnamesrv.cmd
start mqbroker.cmd
Code application
- springboot integrates RocketMq dependencies
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.2.3</version> </dependency>
- yml file configuration
rocketmq: name-server: 127.0.0.1:9876 # producer producer: group: MyProducerGroup #Message sending timeout send-message-timeout: 3000 #Maximum message length 4M max-message-size: 4096 #Number of retries for failed message sending retry-times-when-send-failed: 3 #Number of retries for failed asynchronous message sending retry-times-when-send-async-failed: 2 #consumer consumer: group: MyConsumerGroup #Maximum number of messages fetched each time pull-batch-size: 5
- Code implementation
3.1 Producer sends customized messages
3.1.1 Rewrite defaultMqProducer
3.1.2 Delay in sending messages can be set by message.setDelayTimeSec(timeValue); timeValue delays sending time in seconds type Long
3.1.3 Scheduled delivery, current time + milliseconds
message.setDeliverTimeMs(System.currentTimeMillis() + timeValue); timeValue delays sending time in milliseconds type Long
3.2 Consumers monitor messages
Extension-RocketMq Console
Download address: https://rocketmq.apache.org/download
Install the RocketMQ console to view and analyze message traces