Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very minimal nats code and ultra minimal config. Ready to start writing tests and an example to see if this does anything.
- Loading branch information
Stephen Asbury
committed
Jun 21, 2019
1 parent
a8976f4
commit beea4c6
Showing
6 changed files
with
264 additions
and
23 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
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
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
111 changes: 111 additions & 0 deletions
111
...-nats/src/main/java/org/springframework/cloud/stream/binder/nats/NatsMessageProducer.java
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,111 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.nats; | ||
|
||
import io.nats.client.Connection; | ||
import io.nats.client.Dispatcher; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.context.Lifecycle; | ||
import org.springframework.integration.core.MessageProducer; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.support.GenericMessage; | ||
|
||
public class NatsMessageProducer implements MessageProducer, Lifecycle { | ||
private static final Log logger = LogFactory.getLog(NatsMessageHandler.class); | ||
|
||
private NatsConsumerDestination destination; | ||
private Connection connection; | ||
private MessageChannel output; | ||
private Dispatcher dispatcher; | ||
|
||
public NatsMessageProducer() { | ||
} | ||
|
||
public NatsMessageProducer(NatsConsumerDestination destination, Connection nc) { | ||
this.destination = destination; | ||
this.connection = nc; | ||
} | ||
|
||
public NatsConsumerDestination getDestination() { | ||
return this.destination; | ||
} | ||
|
||
public void setDestination(NatsConsumerDestination destination) { | ||
this.destination = destination; | ||
} | ||
|
||
public Connection getConnection() { | ||
return this.connection; | ||
} | ||
|
||
public void setConnection(Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public void setOutputChannel(MessageChannel outputChannel) { | ||
this.output = outputChannel; | ||
} | ||
|
||
@Override | ||
public MessageChannel getOutputChannel() { | ||
return this.output; | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
return this.dispatcher != null; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (this.dispatcher != null) { | ||
return; | ||
} | ||
|
||
this.dispatcher = this.connection.createDispatcher((msg) -> { | ||
try { | ||
this.output.send(new GenericMessage<byte[]>(msg.getData())); | ||
} | ||
catch (Exception e) { | ||
logger.warn("exception sending message to output channel", e); | ||
} | ||
}); | ||
|
||
String sub = this.destination.getSubject(); | ||
String queue = this.destination.getQueueGroup(); | ||
|
||
if (queue != null && queue.length() > 0) { | ||
this.dispatcher.subscribe(sub, queue); | ||
} | ||
else { | ||
this.dispatcher.subscribe(sub); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (this.dispatcher == null) { | ||
return; | ||
} | ||
|
||
this.connection.closeDispatcher(this.dispatcher); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...er-nats/src/main/java/org/springframework/cloud/stream/binder/nats/NatsMessageSource.java
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,100 @@ | ||
/* | ||
* Copyright 2017-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.nats; | ||
|
||
import java.time.Duration; | ||
|
||
import io.nats.client.Connection; | ||
import io.nats.client.Message; | ||
import io.nats.client.Subscription; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.context.Lifecycle; | ||
import org.springframework.integration.core.MessageProducer; | ||
import org.springframework.integration.endpoint.AbstractMessageSource; | ||
import org.springframework.messaging.MessageChannel; | ||
|
||
public class NatsMessageSource extends AbstractMessageSource<Object> implements Lifecycle { | ||
private static final Log logger = LogFactory.getLog(NatsMessageHandler.class); | ||
|
||
private NatsConsumerDestination destination; | ||
private Connection connection; | ||
private Subscription sub; | ||
|
||
public NatsMessageSource(NatsConsumerDestination destination, Connection nc) { | ||
this.destination = destination; | ||
this.connection = nc; | ||
} | ||
|
||
@Override | ||
protected Object doReceive() { | ||
if (this.sub == null) { | ||
return null; | ||
} | ||
|
||
try { | ||
Message m = this.sub.nextMessage(Duration.ZERO); | ||
|
||
if (m != null) { | ||
return m.getData(); | ||
} | ||
} | ||
catch (InterruptedException exp) { | ||
logger.info("wait for message interrupted"); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
return this.sub != null; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (this.sub != null) { | ||
return; | ||
} | ||
|
||
String sub = this.destination.getSubject(); | ||
String queue = this.destination.getQueueGroup(); | ||
|
||
if (queue != null && queue.length() > 0) { | ||
this.sub = this.connection.subscribe(sub, queue); | ||
} | ||
else { | ||
this.sub = this.connection.subscribe(sub); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (this.sub == null) { | ||
return; | ||
} | ||
|
||
this.sub.unsubscribe(); | ||
} | ||
|
||
@Override | ||
public String getComponentType() { | ||
return "nats:message-source"; | ||
} | ||
} |