Skip to content

Commit

Permalink
refactoring by codebeat #3049
Browse files Browse the repository at this point in the history
  • Loading branch information
yurake committed Feb 15, 2023
1 parent ac6c117 commit 3b3ba06
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public static void stopReceived() {
public MsgBean putMsg() throws NoSuchAlgorithmException {
MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Put");
String body = MsgUtils.createBody(msgbean, splitkey);
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE)) {
try (JMSContext context = createContext()) {
context.createProducer().send(context.createQueue(queuename),
context.createTextMessage(body));
}
Expand All @@ -80,10 +79,7 @@ public MsgBean putMsg() throws NoSuchAlgorithmException {
@Override
public MsgBean getMsg() throws RuntimeException {
MsgBean msgbean = new MsgBean(0, "No Data.", "Get");
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = context
.createConsumer(context.createQueue(queuename))) {
try (JMSConsumer consumer = createConsumerQueue(createContext())) {
String resp = consumer.receiveBody(String.class);

if (!Objects.isNull(resp)) {
Expand All @@ -99,27 +95,35 @@ public MsgBean getMsg() throws RuntimeException {
public MsgBean publishMsg() throws NoSuchAlgorithmException {
MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Publish");
String body = MsgUtils.createBody(msgbean, splitkey);
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE)) {
try (JMSContext context = createContext()) {
context.createProducer().send(context.createTopic(topicname),
context.createTextMessage(body));
}
}
logger.log(Level.INFO, msgbean.getFullmsg());
return msgbean;
}

@Override
public void run() {
while (isEnableReceived) {
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = context
.createConsumer(context.createTopic(topicname))) {
try (JMSConsumer consumer = createConsumerTopic(createContext())) {
amqconsumer.consume(consumer);
} catch (Exception e) {
logger.log(Level.SEVERE, "Subscribe Error.", e);
stopReceived();
}
}
}

private JMSContext createContext() {
return connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE);
}

private JMSConsumer createConsumerTopic(JMSContext context) {
return context.createConsumer(context.createTopic(topicname));
}

private JMSConsumer createConsumerQueue(JMSContext context) {
return context.createConsumer(context.createQueue(queuename));
}
}

0 comments on commit 3b3ba06

Please sign in to comment.