Skip to content

Commit 3dc2891

Browse files
MongoDB Exchange Store YAML Example (#2528)
* enhance: improve YAML parsing error handling with specific messages for missing classes and shorten output * refactor: streamline imports in `GenericYamlParser`, improve exception formatting, and update MongoDB driver instructions in README * refactor: simplify YAML parsing error log by removing configuration details --------- Co-authored-by: Christian Gördes <118011644+christiangoerdes@users.noreply.github.com>
1 parent 481e293 commit 3dc2891

4 files changed

Lines changed: 49 additions & 29 deletions

File tree

annot/src/main/java/com/predic8/membrane/annot/yaml/GenericYamlParser.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515

1616
import com.fasterxml.jackson.core.*;
1717
import com.fasterxml.jackson.databind.*;
18-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
19-
import com.fasterxml.jackson.databind.node.ObjectNode;
18+
import com.fasterxml.jackson.databind.node.*;
2019
import com.networknt.schema.*;
2120
import com.networknt.schema.Error;
2221
import com.predic8.membrane.annot.*;
23-
import com.predic8.membrane.annot.beanregistry.BeanDefinition;
24-
import com.predic8.membrane.annot.beanregistry.BeanRegistry;
22+
import com.predic8.membrane.annot.beanregistry.*;
2523
import org.jetbrains.annotations.*;
2624
import org.slf4j.*;
2725

@@ -189,7 +187,17 @@ public static <T> T createAndPopulateNode(ParsingContext ctx, Class<T> clazz, Js
189187
if (!required.isEmpty())
190188
throw new ParsingException("Missing required fields: " + required.stream().map(McYamlIntrospector::getSetterName).toList(), node);
191189
return configObj;
192-
} catch (Throwable cause) {
190+
}
191+
catch (NoClassDefFoundError e) {
192+
if (e.getCause() != null) {
193+
var missingClass = e.getCause().getMessage(); // TODO: Better use ExceptionUtil.getRootCause() but it isn't visible in annot.
194+
var msg = "Could not create bean with class: %s\nMissing class: %s\n".formatted(clazz, missingClass);
195+
log.error(msg);
196+
throw new ParsingException(msg, node); // TODO: Cause we know the reason, shorten output.
197+
}
198+
throw new ParsingException(e, node);
199+
}
200+
catch (Throwable cause) {
193201
throw new ParsingException(cause, node);
194202
}
195203
}

distribution/examples/extending-membrane/mongo-exchange-store/README.md

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# MongoDB Exchange Store
22

3-
Track and store all your Exchanges, even after restarts by connecting Membrane to MongoDB. View live data in the
4-
Admin Console. Great for debugging, audits, or traffic insights.
5-
This quick guide shows you how to set it up in minutes.
3+
Make exchanges persistent with MongoDB. You can search and inspect the HTTP traffic later even after restarts.
4+
View live data in the Admin Console from the MongoDB store. Great for debugging, audits, or traffic insights.
65

76
### Prerequisite
87

@@ -18,26 +17,12 @@ This quick guide shows you how to set it up in minutes.
1817

1918
1. **Download MongoDB Driver:**
2019

21-
- Download the MongoDB
22-
driver [https://repo1.maven.org/maven2/org/mongodb/mongodb-driver-sync/5.0.1/mongodb-driver-sync-5.0.1.jar](https://repo1.maven.org/maven2/org/mongodb/mongodb-driver-sync/5.0.1/mongodb-driver-sync-5.0.1.jar).
23-
- Place it in the `lib` directory of your Membrane installation.
24-
25-
2. **Configure `proxies.xml`:**
26-
27-
- Example configuration for MongoDB:
28-
29-
```xml
30-
<mongoDBExchangeStore id="store" connection="mongodb://localhost:27017/" database="membrane"
31-
collection="exchanges" />
32-
<router exchangeStore="store">
33-
<serviceProxy name="predic8.com" port="2000">
34-
<target url="https://membrane-soa.org" />
35-
</serviceProxy>
36-
<serviceProxy port="9000">
37-
<adminConsole />
38-
</serviceProxy>
39-
</router>
40-
```
20+
- Choose from:
21+
[https://repo1.maven.org/maven2/org/mongodb/mongodb-driver-sync/](https://repo1.maven.org/maven2/org/mongodb/mongodb-driver-sync/)
22+
a recent version. 5.X is recommended.
23+
- Place it in the `lib` directory of your Membrane installation.
24+
25+
2. **Configure:** edit `apis.yaml`
4126

4227
3. **run service.proxy.sh script:**
4328

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.5.json
2+
3+
components:
4+
my-store:
5+
mongoDBExchangeStore:
6+
connection: mongodb://localhost:27017/
7+
collection: exchange
8+
database: exchange
9+
10+
---
11+
12+
api:
13+
name: predic8
14+
port: 2000
15+
target:
16+
url: https://api.predic8.de
17+
18+
---
19+
20+
api:
21+
port: 9000
22+
flow:
23+
- adminConsole: {}

docs/ROADMAP.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
- Documentation
2323
- See JmxExporter
2424
- Synchronization of BeanRegistry
25+
- Move YAML Parsing to core?
26+
- Core isn't visible.
27+
- YAML parsing:
28+
- When the reason for a parse error is clear. Shorten error message.
29+
2530
- BalancerHealthMonitor:
2631
- @PostConstruct instead of InitializingBean, DisposableBean
2732
- Scripting: expose beanRegistry
28-
2933
- IfInterceptor:
3034
- Add "else"
3135

0 commit comments

Comments
 (0)