Skip to content

Commit f4762a6

Browse files
split producers anad consumers
1 parent 18074ab commit f4762a6

File tree

11 files changed

+191
-64
lines changed

11 files changed

+191
-64
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.openubl.exceptions;
2+
3+
public class InvalidXMLFileException extends Exception {
4+
public InvalidXMLFileException(Exception e) {
5+
super(e);
6+
}
7+
}

src/main/java/org/openubl/jms/SendCallbackJMSConsumer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.quarkus.runtime.ShutdownEvent;
55
import io.quarkus.runtime.StartupEvent;
66
import org.eclipse.microprofile.config.inject.ConfigProperty;
7+
import org.jboss.logging.Logger;
78
import org.openubl.factories.ModelFactory;
89
import org.openubl.providers.CallbackRSProvider;
910

@@ -19,6 +20,8 @@
1920
@ApplicationScoped
2021
public class SendCallbackJMSConsumer implements Runnable {
2122

23+
private static final Logger LOG = Logger.getLogger(SendCallbackJMSConsumer.class);
24+
2225
@Inject
2326
CallbackRSProvider callbackRSProvider;
2427

@@ -40,7 +43,7 @@ void onStop(@Observes ShutdownEvent ev) {
4043

4144
@Override
4245
public void run() {
43-
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
46+
try (JMSContext context = connectionFactory.createContext(Session.CLIENT_ACKNOWLEDGE)) {
4447
JMSConsumer jmsConsumer = context.createConsumer(context.createQueue(callbackQueue));
4548
while (true) {
4649
Message message = jmsConsumer.receive();
@@ -51,14 +54,19 @@ public void run() {
5154
BillServiceModel billServiceModel = ModelFactory.getBillServiceModel(message);
5255
if (message instanceof BytesMessage) {
5356
billServiceModel.setCdr(message.getBody(byte[].class));
54-
} else if (message instanceof TextMessage) {
57+
} else {
5558
billServiceModel.setCdr(null);
5659
}
5760

58-
callbackRSProvider.sendCallback(billServiceModel);
61+
boolean result = callbackRSProvider.sendCallback(billServiceModel);
62+
if (result) {
63+
message.acknowledge();
64+
}
5965
}
6066
} catch (JMSException e) {
61-
throw new RuntimeException(e);
67+
LOG.error(e);
68+
} catch (Throwable e) {
69+
LOG.error("Unexpected exception", e);
6270
}
6371
}
6472

src/main/java/org/openubl/jms/DefaultJMSProducer.java renamed to src/main/java/org/openubl/jms/SendCallbackJMSProducer.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,50 @@
22

33
import io.github.carlosthe19916.webservices.providers.BillServiceModel;
44
import org.eclipse.microprofile.config.inject.ConfigProperty;
5+
import org.jboss.logging.Logger;
56
import org.openubl.factories.ModelFactory;
67
import org.openubl.models.SendFileModel;
78

8-
import javax.annotation.PostConstruct;
9-
import javax.annotation.PreDestroy;
109
import javax.enterprise.context.ApplicationScoped;
11-
import javax.enterprise.context.Destroyed;
1210
import javax.inject.Inject;
1311
import javax.jms.*;
12+
import java.lang.IllegalStateException;
1413
import java.util.Map;
1514

1615
@ApplicationScoped
17-
public class DefaultJMSProducer {
16+
public class SendCallbackJMSProducer {
1817

19-
@ConfigProperty(name = "openubl.sendFileQueue")
20-
String sendFileQueue;
18+
private static final Logger LOG = Logger.getLogger(SendCallbackJMSProducer.class);
2119

2220
@ConfigProperty(name = "openubl.callbackQueue")
2321
String callbackQueue;
2422

2523
@Inject
2624
ConnectionFactory connectionFactory;
2725

28-
public void produceSendFileMessage(SendFileModel sendFileModel, byte[] file) {
29-
sendBytesMessage(sendFileQueue, ModelFactory.getAsMap(sendFileModel), file);
30-
}
31-
3226
public void produceSendCallbackMessage(BillServiceModel billServiceModel) {
33-
sendBytesMessage(callbackQueue, ModelFactory.getAsMap(billServiceModel), billServiceModel.getCdr());
34-
}
35-
36-
private void sendBytesMessage(String queueName, Map<String, String> properties, byte[] bytes) {
3727
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
3828
JMSProducer jmsProducer = context.createProducer();
3929

40-
Queue queue = context.createQueue(queueName);
30+
Queue queue = context.createQueue(callbackQueue);
4131

4232
Message message;
43-
if (bytes != null) {
33+
if (billServiceModel.getCdr() != null) {
4434
BytesMessage bytesMessage = context.createBytesMessage();
45-
bytesMessage.writeBytes(bytes);
35+
bytesMessage.writeBytes(billServiceModel.getCdr());
4636

4737
message = bytesMessage;
4838
} else {
49-
message = context.createTextMessage();
39+
message = context.createTextMessage("No CDR available");
5040
}
5141

52-
for (Map.Entry<String, String> entry : properties.entrySet()) {
42+
for (Map.Entry<String, String> entry : ModelFactory.getAsMap(billServiceModel).entrySet()) {
5343
message.setStringProperty(entry.getKey(), entry.getValue());
5444
}
5545

5646
jmsProducer.send(queue, message);
57-
} catch (Throwable ex) {
58-
// handle exception (details omitted)
59-
System.out.println(ex);
47+
} catch (JMSException e) {
48+
LOG.error("Error trying to send bytes message", e);
6049
}
6150
}
6251

src/main/java/org/openubl/jms/SendFileJMSConsumer.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.quarkus.runtime.ShutdownEvent;
44
import io.quarkus.runtime.StartupEvent;
55
import org.eclipse.microprofile.config.inject.ConfigProperty;
6+
import org.jboss.logging.Logger;
7+
import org.openubl.exceptions.InvalidXMLFileException;
68
import org.openubl.factories.ModelFactory;
79
import org.openubl.models.SendFileModel;
810
import org.openubl.providers.SendFileWSProvider;
@@ -19,6 +21,8 @@
1921
@ApplicationScoped
2022
public class SendFileJMSConsumer implements Runnable {
2123

24+
private static final Logger LOG = Logger.getLogger(SendFileJMSConsumer.class);
25+
2226
@Inject
2327
SendFileWSProvider sunatWSProvider;
2428

@@ -40,23 +44,29 @@ void onStop(@Observes ShutdownEvent ev) {
4044

4145
@Override
4246
public void run() {
43-
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
47+
try (JMSContext context = connectionFactory.createContext(Session.CLIENT_ACKNOWLEDGE)) {
4448
JMSConsumer jmsConsumer = context.createConsumer(context.createQueue(sendFileQueue));
4549
while (true) {
4650
Message message = jmsConsumer.receive();
4751
if (message == null) {
4852
return;
4953
}
5054

51-
if (message instanceof BytesMessage) {
52-
SendFileModel model = ModelFactory.getSendFilePropertiesModel(message);
53-
sunatWSProvider.sendFile(model, message.getBody(byte[].class));
54-
} else if (message instanceof TextMessage) {
55-
System.out.println("SendFileJMSConsumer can not send empty bytes[]");
55+
if (!(message instanceof BytesMessage)) {
56+
LOG.warn("Consumer can not consume messages other than Bytes Messages");
57+
}
58+
59+
SendFileModel model = ModelFactory.getSendFilePropertiesModel(message);
60+
boolean result = sunatWSProvider.sendFile(model, message.getBody(byte[].class));
61+
62+
if (result) {
63+
message.acknowledge();
5664
}
5765
}
66+
} catch (JMSException e) {
67+
LOG.error(e);
5868
} catch (Throwable e) {
59-
System.out.println(e);
69+
LOG.error("Unexpected exception", e);
6070
}
6171
}
6272

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.openubl.jms;
2+
3+
import io.github.carlosthe19916.webservices.providers.BillServiceModel;
4+
import org.eclipse.microprofile.config.inject.ConfigProperty;
5+
import org.jboss.logging.Logger;
6+
import org.openubl.factories.ModelFactory;
7+
import org.openubl.models.SendFileModel;
8+
9+
import javax.enterprise.context.ApplicationScoped;
10+
import javax.inject.Inject;
11+
import javax.jms.*;
12+
import java.lang.IllegalStateException;
13+
import java.util.Map;
14+
15+
@ApplicationScoped
16+
public class SendFileJMSProducer {
17+
18+
private static final Logger LOG = Logger.getLogger(SendFileJMSProducer.class);
19+
20+
@ConfigProperty(name = "openubl.sendFileQueue")
21+
String sendFileQueue;
22+
23+
@Inject
24+
ConnectionFactory connectionFactory;
25+
26+
public void produceSendFileMessage(SendFileModel sendFileModel, byte[] file) {
27+
if (file == null || file.length == 0) {
28+
throw new IllegalStateException("Invalid file");
29+
}
30+
31+
try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE)) {
32+
JMSProducer jmsProducer = context.createProducer();
33+
34+
Queue queue = context.createQueue(sendFileQueue);
35+
36+
BytesMessage message = context.createBytesMessage();
37+
message.writeBytes(file);
38+
39+
for (Map.Entry<String, String> entry : ModelFactory.getAsMap(sendFileModel).entrySet()) {
40+
message.setStringProperty(entry.getKey(), entry.getValue());
41+
}
42+
43+
jmsProducer.send(queue, message);
44+
} catch (JMSException e) {
45+
LOG.error("Error trying to send bytes message", e);
46+
}
47+
}
48+
49+
50+
}

src/main/java/org/openubl/providers/CallbackRSProvider.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@
33
import io.github.carlosthe19916.webservices.providers.BillServiceModel;
44
import org.eclipse.microprofile.config.inject.ConfigProperty;
55
import org.eclipse.microprofile.rest.client.inject.RestClient;
6+
import org.jboss.logging.Logger;
7+
import org.openubl.jms.SendCallbackJMSConsumer;
68
import org.openubl.models.MultipartBody;
79
import org.openubl.resources.client.CallbackMultipartService;
810

911
import javax.enterprise.context.ApplicationScoped;
1012
import javax.inject.Inject;
13+
import javax.ws.rs.core.Response;
1114
import java.io.ByteArrayInputStream;
1215
import java.nio.charset.StandardCharsets;
1316

1417
@ApplicationScoped
1518
public class CallbackRSProvider {
1619

17-
@ConfigProperty(name = "org.openubl.resources.client.MultiCallbackMultipartService/mp-rest/url")
20+
private static final Logger LOG = Logger.getLogger(CallbackRSProvider.class);
21+
22+
@ConfigProperty(name = "org.openubl.enableCallback")
23+
boolean enableCallback;
24+
25+
@ConfigProperty(name = "org.openubl.resources.client.CallbackMultipartService/mp-rest/url")
1826
String callbackUrl;
1927

2028
@Inject
2129
@RestClient
2230
CallbackMultipartService service;
2331

24-
public void sendCallback(BillServiceModel billServiceModel) {
25-
if (callbackUrl == null) {
26-
return;
32+
public boolean sendCallback(BillServiceModel billServiceModel) {
33+
if (!enableCallback || callbackUrl == null) {
34+
LOG.info("Not sending message to client");
35+
return true;
2736
}
2837

2938
MultipartBody body = new MultipartBody();
@@ -33,10 +42,17 @@ public void sendCallback(BillServiceModel billServiceModel) {
3342
body.status = billServiceModel.getStatus().toString();
3443

3544
if (billServiceModel.getCdr() != null) {
36-
body.crd = new ByteArrayInputStream("HELLO WORLD".getBytes(StandardCharsets.UTF_8));
45+
body.crd = new ByteArrayInputStream(billServiceModel.getCdr());
46+
}
47+
48+
try {
49+
service.sendMultipartData(body);
50+
} catch (Exception e) {
51+
LOG.error("Could not send to client");
52+
return false;
3753
}
3854

39-
service.sendMultipartData(body);
55+
return true;
4056
}
4157

4258
}

src/main/java/org/openubl/providers/SendFileProvider.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.openubl.providers;
22

33
import org.eclipse.microprofile.config.inject.ConfigProperty;
4-
import org.openubl.jms.DefaultJMSProducer;
4+
import org.openubl.exceptions.InvalidXMLFileException;
5+
import org.openubl.jms.SendFileJMSProducer;
56
import org.openubl.models.SendFileModel;
67
import org.openubl.models.DocumentType;
78
import org.openubl.xml.SunatDocumentModel;
@@ -26,13 +27,23 @@ public class SendFileProvider {
2627
String sunatUrl1;
2728

2829
@Inject
29-
DefaultJMSProducer sunatJMSProducer;
30+
SendFileJMSProducer sunatJMSProducer;
3031

3132
@Inject
3233
SunatDocumentProvider sunatDocumentProvider;
3334

34-
public void sendFile(byte[] file, String username, String password) throws IOException, SAXException, ParserConfigurationException {
35-
SunatDocumentModel sunatDocument = sunatDocumentProvider.getSunatDocument(new ByteArrayInputStream(file));
35+
/**
36+
* @param file file to be sent to SUNAT
37+
* @param username username in SUNAT
38+
* @param password password in SUNAT
39+
*/
40+
public void sendFile(byte[] file, String username, String password) throws InvalidXMLFileException {
41+
SunatDocumentModel sunatDocument;
42+
try {
43+
sunatDocument = sunatDocumentProvider.getSunatDocument(new ByteArrayInputStream(file));
44+
} catch (ParserConfigurationException | SAXException | IOException e) {
45+
throw new InvalidXMLFileException(e);
46+
}
3647

3748
String serverUrl = getServerUrl(sunatDocument.getDocumentType());
3849
String fileName = getFileName(sunatDocument.getDocumentType(), sunatDocument.getRuc(), sunatDocument.getDocumentID());

0 commit comments

Comments
 (0)