how tibco ems and kafka replay queue messages

1.Tibco EMS queue messages 可以通过GEMS操作copy,但最多只能读取1000条,可以用下列代码进行大量message压测

需要 tibjms.jar  & tibjmsadmin.jar

App.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import javax.jms.JMSException;
import com.tibco.tibjms.admin.TibjmsAdminException;
 
public   class App {
 
 
    public static void main(String[] args) throws TibjmsAdminException, JMSException {
 
 
            EmsHelper emsHelper = new EmsHelper("tcp://localhost:7222",null,null);
            String m_dest = "destqueue";
            String m_from = "fromqueue";
            int m_maxMsgs = 10000;
            emsHelper.ShowConsumerInfos("testqueue");
            emsHelper.CopyQueueMsgs(m_from,m_dest,m_maxMsgs);
             
            emsHelper.Dispose();
 
 
    }
     
 
}

  

EMSHelper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.example.tibco;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.stream.Collectors;
 
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TopicSubscriber;
 
 
import com.tibco.gems.GemsQueueBrowser;
import com.tibco.tibjms.Tibjms;
import com.tibco.tibjms.TibjmsQueueConnectionFactory;
import com.tibco.tibjms.admin.BridgeInfo;
import com.tibco.tibjms.admin.BridgeTarget;
import com.tibco.tibjms.admin.ConnectionFactoryInfo;
import com.tibco.tibjms.admin.ConnectionInfo;
import com.tibco.tibjms.admin.ConsumerInfo;
import com.tibco.tibjms.admin.QueueInfo;
import com.tibco.tibjms.admin.TibjmsAdmin;
import com.tibco.tibjms.admin.TibjmsAdminException;
import com.tibco.tibjms.admin.TibjmsAdminInvalidNameException;
 
// tibjms.jar  & tibjmsadmin.jar
 
public class EmsHelper {
    protected String m_url = "tcp://localhost:7222";
    protected   String m_user = "admin";     
    protected  String m_password;
    protected  String m_dest = "testqueue";
    protected  String m_from = "testqueue";
    protected  String m_topic = "testqueue";
    protected  String m_logDir = "./log";
    protected QueueConnection m_connection = null;
    protected Session m_session = null;
    protected MessageProducer m_msgProducer = null;
    protected Destination m_jmsdest = null;
    protected TopicSubscriber m_subscriber = null;
    protected boolean m_noLocal = false;
    protected QueueBrowser m_browser = null;
    protected String m_selector = "";
    protected Enumeration m_msgsEnum = null;
    protected int m_maxMsgs = 10000;
    protected TibjmsAdmin admin = null;
    Map<Long, ConnectionInfo> connectionInfoMap = null;
    //protected  Map m_sslParams = new Hashtable();
     
 
    public  EmsHelper(String url , String user , String password)  throws JMSException, TibjmsAdminException {
         if(url!= null) this.m_url = url;
         if(user!= null) this.m_user = user;
         if(password!= null) this.m_password = password;
         admin = new TibjmsAdmin(this.m_url,this.m_user, this.m_password);
         /*
         ConnectionFactoryInfo[] connectionFactories = admin.getConnectionFactories();
         for(ConnectionFactoryInfo connInfo : connectionFactories) {
                System.out.println("ConnectionFactoryInfo : "+connInfo.toString());
            }
         ConnectionInfo[] connectionInfos = admin.getSystemConnections();
         for(ConnectionInfo connInfo : connectionInfos) {
                System.out.println("ConnectionInfo : "+connInfo.toString());
            }
         */
         ConnectionInfo[] connectionInfos = admin.getConnections();
         /*
         for(ConnectionInfo connInfo : connectionInfos2) {
                System.out.println("ConnectionInfo : "+connInfo.toString());
            }
         */
          
         java.util.List<ConnectionInfo> conns = Arrays.asList(connectionInfos);
         connectionInfoMap = conns.stream().collect(Collectors.toMap(ConnectionInfo::getID, o->o));
    }
     
    private void ShowOutput(String msg) {
        System.out.println(msg);
    }
     
    public void CopyQueueMsgs(String from,String dest,int size) throws JMSException {
        if(from!= null) this.m_from = from;
        if(dest!= null) this.m_dest = dest;
        if(size>= 0) this.m_maxMsgs = size;
 
        ShowOutput("Connecting to: " + this.m_url + "\n");
        TibjmsQueueConnectionFactory tibjmsQueueConnectionFactory = new TibjmsQueueConnectionFactory(this.m_url, null, null);   
        this.m_connection = tibjmsQueueConnectionFactory.createQueueConnection(this.m_user, this.m_password);
        this.m_connection.start();
        this.m_session = this.m_connection.createSession(Session.AUTO_ACKNOWLEDGE);
        this.m_msgProducer = this.m_session.createProducer(null);
        this.m_msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
         
 
        Queue queue = this.m_session.createQueue(this.m_from);
        this.m_jmsdest = this.m_session.createQueue(this.m_dest);
        m_selector = "";
        //this.m_browser = this.m_session.createBrowser(queue, m_selector);
        this.m_browser = this.m_session.createBrowser(queue);
        this.m_msgsEnum = this.m_browser.getEnumeration();
         
        ShowOutput("["+size+"]" + from +" -> "+ dest+ "\n" );
        if (this.m_msgsEnum.hasMoreElements()) {
            for (byte b = 0; b < this.m_maxMsgs; b++) {
                Message message1 = (Message)this.m_msgsEnum.nextElement();
                ShowOutput("Copying message: " + message1.getJMSMessageID() + "\n");
                Message message2 = Tibjms.createFromBytes(Tibjms.getAsBytes(message1));
                if (message1 != null) {
                  try {
                      this.m_msgProducer.send(this.m_jmsdest, message2, message1.getJMSDeliveryMode(), message1.getJMSPriority(), message1.getJMSExpiration());
                      ShowOutput("Message sent as: " + message2.getJMSMessageID() + "\n");
                  } catch (JMSException jMSException) {
                    System.err.println("Exception: " + jMSException.getMessage());
                    return;
                  }
                }
            }
       }
    }
     
    public void ShowConsumerInfos(String queueName) throws TibjmsAdminException {
        QueueInfo qi= new QueueInfo(queueName);
        // ShowOutput(qi.toString());
        //Queue queue = this.m_session.createQueue(queueName);
        System.out.println("Queue : "+ queueName);
        ConsumerInfo[] consumerInfos = this.admin.getConsumers(null,null,qi,false,0);
        for (ConsumerInfo info : consumerInfos) {
            System.out.println("getConnectionID : "+info.getConnectionID());
            ConnectionInfo connInfo = connectionInfoMap.get(info.getConnectionID());
            System.out.println("ConnectionInfo : "+connInfo.toString());
            System.out.println("getDetails : "+info.getDetails().toString());
            System.out.println("getPendingMessageCount : "+info.getPendingMessageCount());
        }
         
         
         
         
    }
     
    public void ShowBridgeInfos() throws TibjmsAdminException {
        TibjmsAdmin admin = new TibjmsAdmin(this.m_url,this.m_user, this.m_password);
        BridgeInfo[] BridgeInfos = admin.getBridges();
        for (BridgeInfo bridgeInfo : BridgeInfos) {
            System.out.println(bridgeInfo.getName());
            for (BridgeTarget bridgeTarget : bridgeInfo.getTargets()) {
                System.out.println(bridgeTarget.getName());
            }
        }
    }
     
    public void clearMessages(String queueName) throws TibjmsAdminException, TibjmsAdminInvalidNameException{
        TibjmsAdmin jmsAdmin = new TibjmsAdmin(this.m_url,this.m_user, this.m_password);
        jmsAdmin.purgeQueue(queueName);
        // alternatively purge all queues:
        // jmsAdmin.purgeQueues(">");
    }
     
    public void Dispose() {
        try {
          if (this.m_session != null) {
            this.m_session.close();
            this.m_session = null;
          }
          if (this.m_connection != null) {
            this.m_connection.close();
            this.m_connection = null;
          }
        } catch (JMSException jMSException) {
          System.err.println("Exception: " + jMSException.getMessage());
        }
    }
 
 
}

2.kafka 则可以用另一个group id重新消费,还可以指定offset

CustomConsumerSeek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.atguigu.kafka.consumer;
 
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;
 
import java.time.Duration;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Set;
 
public class CustomConsumerSeek {
 
    public static void main(String[] args) {
 
        // 0 配置信息
        Properties properties = new Properties();
 
        // 连接
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"kafka1:9092,kafka2:9093,kafka3:9094");
 
        // 反序列化
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
 
        // 组id
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"test2");
 
        // 1 创建消费者
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);
 
        // 2 订阅主题
        ArrayList<String> topics = new ArrayList<>();
        topics.add("par-topic");
        kafkaConsumer.subscribe(topics);
 
        // 指定位置进行消费
        Set<TopicPartition> assignment = kafkaConsumer.assignment();
 
        //  保证分区分配方案已经制定完毕
        while (assignment.size() == 0){
            kafkaConsumer.poll(Duration.ofSeconds(1));
 
            assignment = kafkaConsumer.assignment();
        }
 
        // 指定消费的offset
        for (TopicPartition topicPartition : assignment) {
            kafkaConsumer.seek(topicPartition,5);
        }
 
        // 3  消费数据
        while (true){
 
            ConsumerRecords<String, String> consumerRecords = kafkaConsumer.poll(Duration.ofSeconds(1));
 
            for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
 
                System.out.println(consumerRecord);
            }
        }
    }
}

  

  

posted on   白马酒凉  阅读(112)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示