-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
833 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.gradle | ||
/build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
/out/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module.exports = (Handlebars, _) => { | ||
|
||
//capitalizeFirstLetter | ||
Handlebars.registerHelper('capitalize', (str) => { | ||
return _.capitalize(str); | ||
}); | ||
|
||
Handlebars.registerHelper('camelCase', (str) => { | ||
return _.camelCase(str); | ||
}); | ||
|
||
Handlebars.registerHelper('upperFirst', (str) => { | ||
return _.upperFirst(str); | ||
}); | ||
|
||
//convert string to java constant format: lightMeasuredPublish -> LIGHT_MEASURED_PUBLISH | ||
Handlebars.registerHelper('javaConst', (str) => { | ||
return _.snakeCase(str).toUpperCase(); | ||
}); | ||
|
||
Handlebars.registerHelper('compare', (lvalue, operator, rvalue, options) => { | ||
if (arguments.length < 4) throw new Error('Handlerbars Helper "compare" needs 3 parameters'); | ||
|
||
const operators = { | ||
'==': (l,r) => { return l == r; }, | ||
'===': (l,r) => { return l === r; }, | ||
'!=': (l,r) => { return l != r; }, | ||
'<': (l,r) => { return l < r; }, | ||
'>': (l,r) => { return l > r; }, | ||
'<=': (l,r) => { return l <= r; }, | ||
'>=': (l,r) => { return l >= r; }, | ||
typeof: (l,r) => { return typeof l === r; } | ||
}; | ||
|
||
if (!operators[operator]) throw new Error(`Handlerbars Helper 'compare' doesn't know the operator ${operator}`); | ||
const result = operators[operator](lvalue,rvalue); | ||
|
||
if (result) return options.fn(this); | ||
|
||
return options.inverse(this); | ||
}); | ||
|
||
// Handlebars.registerHelper('json', function(context) { | ||
// return JSON.stringify(context); | ||
// }); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.asyncapi.infrastructure; | ||
|
||
import com.asyncapi.service.MessageHandlerService; | ||
import org.springframework.amqp.core.*; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitAdmin; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.integration.amqp.dsl.Amqp; | ||
import org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint; | ||
import org.springframework.integration.annotation.ServiceActivator; | ||
import org.springframework.integration.channel.DirectChannel; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.dsl.IntegrationFlows; | ||
import org.springframework.messaging.MessageChannel; | ||
|
||
@Configuration | ||
public class Config { | ||
|
||
@Value("${amqp.broker.host}") | ||
private String host; | ||
|
||
@Value("${amqp.broker.port}") | ||
private int port; | ||
|
||
@Value("${amqp.broker.username}") | ||
private String username; | ||
|
||
@Value("${amqp.broker.password}") | ||
private String password; | ||
|
||
{{#each asyncapi.topics as |topic key|}} | ||
{{#if topic.subscribe}} | ||
@Value("${amqp.exchange.{{~topic.x-service-name~}}}") | ||
private String {{topic.x-service-name}}Exchange; | ||
|
||
{{/if}} | ||
{{/each}} | ||
{{#each asyncapi.topics as |topic key|}} | ||
{{#if topic.subscribe}} | ||
@Value("${amqp.queue.{{~topic.x-service-name~}}}") | ||
private String {{topic.x-service-name}}Queue; | ||
|
||
{{/if}} | ||
{{/each}} | ||
|
||
@Bean | ||
public ConnectionFactory connectionFactory() { | ||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host); | ||
connectionFactory.setUsername(username); | ||
connectionFactory.setPassword(password); | ||
connectionFactory.setPort(port); | ||
return connectionFactory; | ||
} | ||
|
||
@Bean | ||
public AmqpAdmin amqpAdmin() { | ||
return new RabbitAdmin(connectionFactory()); | ||
} | ||
|
||
@Bean | ||
public Declarables exchanges() { | ||
return new Declarables( | ||
{{#each asyncapi.topics as |topic key|}} | ||
{{#if topic.publish}} | ||
new TopicExchange({{topic.x-service-name}}Exchange, true, false){{#unless @last}},{{/unless}} | ||
{{/if}} | ||
{{/each}} | ||
); | ||
} | ||
|
||
@Bean | ||
public Declarables queues() { | ||
return new Declarables( | ||
{{#each asyncapi.topics as |topic key|}} | ||
{{#if topic.subscribe}} | ||
new Queue({{topic.x-service-name}}Queue, true, false, false){{#unless @last}},{{/unless}} | ||
{{/if}} | ||
{{/each}} | ||
); | ||
} | ||
|
||
// consumer | ||
|
||
@Autowired | ||
MessageHandlerService messageHandlerService; | ||
{{#each asyncapi.topics as |topic key|}} | ||
|
||
{{#if topic.subscribe}} | ||
@Bean | ||
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() { | ||
return IntegrationFlows.from(Amqp.inboundGateway(connectionFactory(), {{topic.x-service-name}}Queue)) | ||
.handle(messageHandlerService::handle{{upperFirst topic.x-service-name}}) | ||
.get(); | ||
} | ||
{{/if}} | ||
{{/each}} | ||
|
||
// publisher | ||
|
||
@Bean | ||
public RabbitTemplate rabbitTemplate() { | ||
RabbitTemplate template = new RabbitTemplate(connectionFactory()); | ||
return template; | ||
} | ||
{{#each asyncapi.topics as |topic key|}} | ||
|
||
{{#if topic.publish}} | ||
@Bean | ||
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() { | ||
return new DirectChannel(); | ||
} | ||
|
||
@Bean | ||
@ServiceActivator(inputChannel = "{{camelCase topic.x-service-name}}OutboundChannel") | ||
public AmqpOutboundEndpoint {{camelCase topic.x-service-name}}Outbound(AmqpTemplate amqpTemplate) { | ||
AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate); | ||
outbound.setExchangeName({{topic.x-service-name}}Exchange); | ||
outbound.setRoutingKey("#"); | ||
return outbound; | ||
} | ||
{{/if}} | ||
{{/each}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.asyncapi.infrastructure; | ||
|
||
import com.asyncapi.service.MessageHandlerService; | ||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.integration.annotation.ServiceActivator; | ||
import org.springframework.integration.channel.DirectChannel; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.dsl.IntegrationFlows; | ||
import org.springframework.integration.endpoint.MessageProducerSupport; | ||
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; | ||
import org.springframework.integration.mqtt.core.MqttPahoClientFactory; | ||
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; | ||
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler; | ||
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.MessageHandler; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Configuration | ||
public class Config { | ||
|
||
@Value("${mqtt.broker.host}") | ||
private String host; | ||
|
||
@Value("${mqtt.broker.port}") | ||
private int port; | ||
|
||
@Value("${mqtt.broker.username}") | ||
private String username; | ||
|
||
@Value("${mqtt.broker.password}") | ||
private String password; | ||
|
||
{{#each asyncapi.topics as |topic key|}} | ||
{{#if topic.subscribe}} | ||
@Value("${mqtt.topic.{{~topic.x-service-name~}}Topic}") | ||
private String {{topic.x-service-name}}Topic; | ||
|
||
{{/if}} | ||
{{/each}} | ||
|
||
@Bean | ||
public MqttPahoClientFactory mqttClientFactory() { | ||
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); | ||
MqttConnectOptions options = new MqttConnectOptions(); | ||
options.setServerURIs(new String[] { host + ":" + port }); | ||
if (!StringUtils.isEmpty(username)) { | ||
options.setUserName(username); | ||
} | ||
if (!StringUtils.isEmpty(password)) { | ||
options.setPassword(password.toCharArray()); | ||
} | ||
factory.setConnectionOptions(options); | ||
return factory; | ||
} | ||
|
||
// consumer | ||
|
||
@Autowired | ||
MessageHandlerService messageHandlerService; | ||
{{#each asyncapi.topics as |topic key|}} | ||
|
||
{{#if topic.subscribe}} | ||
@Bean | ||
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() { | ||
return IntegrationFlows.from({{camelCase topic.x-service-name}}Inbound()) | ||
.handle(messageHandlerService::handle{{upperFirst topic.x-service-name}}) | ||
.get(); | ||
} | ||
|
||
@Bean | ||
public MessageProducerSupport {{camelCase topic.x-service-name}}Inbound() { | ||
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("{{camelCase topic.x-service-name}}Subscriber", | ||
mqttClientFactory(), {{topic.x-service-name}}Topic); | ||
adapter.setCompletionTimeout(5000); | ||
adapter.setConverter(new DefaultPahoMessageConverter()); | ||
return adapter; | ||
} | ||
{{/if}} | ||
{{/each}} | ||
|
||
// publisher | ||
{{#each asyncapi.topics as |topic key|}} | ||
|
||
{{#if topic.publish}} | ||
@Bean | ||
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() { | ||
return new DirectChannel(); | ||
} | ||
|
||
@Bean | ||
@ServiceActivator(inputChannel = "{{camelCase topic.x-service-name}}OutboundChannel") | ||
public MessageHandler {{camelCase topic.x-service-name}}Outbound() { | ||
MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler("{{camelCase topic.x-service-name}}Publisher", mqttClientFactory()); | ||
pahoMessageHandler.setAsync(true); | ||
pahoMessageHandler.setDefaultTopic({{topic.x-service-name}}Topic); | ||
return pahoMessageHandler; | ||
} | ||
{{/if}} | ||
{{/each}} | ||
|
||
} |
Oops, something went wrong.