Skip to content

Commit

Permalink
initial cut of a websocket listener
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaslin committed Oct 4, 2014
1 parent 391ea55 commit 72a24f5
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 2 deletions.
26 changes: 26 additions & 0 deletions echo-stomp/echo-stomp.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2014 Netflix, Inc.
*
* 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
*
* http://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.
*/

apply from: "$rootDir/gradle/groovy-module.gradle"

dependencies {
compile project(':echo-model')
compile 'org.springframework.boot:spring-boot-starter-websocket:1.1.7.RELEASE'
compile 'org.springframework:spring-messaging:4.0.7.RELEASE'
testCompile commonDependencies.spockSpring
testCompile commonDependencies.springTest
testCompile commonDependencies.objenesis
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2014 Netflix, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.echo.config

import org.springframework.context.annotation.Configuration
import org.springframework.messaging.simp.config.ChannelRegistration
import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
import org.springframework.web.socket.config.annotation.StompEndpointRegistry

/**
* Configuration for WebSockets.
*/
@Configuration
@EnableWebSocketMessageBroker
class StompConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint('/events').withSockJS()
}

@Override
void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
channelRegistration.taskExecutor().corePoolSize(10).maxPoolSize(100)
}

@Override
void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker('/topic/')
config.setApplicationDestinationPrefixes('/ws')
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2014 Netflix, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.echo.events

import com.netflix.spinnaker.echo.model.Event
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.stereotype.Component

/**
* Event listener for stomp
*/
@Component
class StompEventListener implements EchoEventListener {

@Autowired
SimpMessagingTemplate template

@Override
void processEvent(Event event) {

if (event.details.application) {
template.convertAndSend(
'/topic/application/' + event.details.application,
event
)
}

template.convertAndSend(
'/topic/source/' + event.details.source,
event
)

template.convertAndSend(
'/topic/type/' + event.details.type,
event
)

template.convertAndSend(
'/topic/events',
event
)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2014 Netflix, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.echo.events

import com.netflix.spinnaker.echo.model.Event
import org.springframework.messaging.simp.SimpMessagingTemplate
import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Unroll

/**
* tests for StompEventListener
*/
@Unroll
class StompEventListenerSpec extends Specification {

@Subject
StompEventListener listener = new StompEventListener()
Event e

void setup() {
listener.template = Mock(SimpMessagingTemplate)
e = new Event(details: [])
}

void 'messages are sent to #type topic'() {
given:
e.details[type] = 'mytype'

when:
listener.processEvent(e)

then:
1 * listener.template.convertAndSend("/topic/$type/mytype", _)

where:
type << ['application', 'source', 'type']
}

void 'no event is fired into the websocket endpoint if no application is specified'() {
expect:
e.details.application == null

when:
listener.processEvent(e)

then:
0 * listener.template.convertAndSend( { it.startsWith('/topic/application') } , _)
}

void 'all events are relayed to events topic'() {
when:
listener.processEvent(e)

then:
1 * listener.template.convertAndSend('/topic/events', _)
}

}
2 changes: 1 addition & 1 deletion echo-web/echo-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
compile project(':echo-core')
compile project(':echo-model')
compile project(':echo-cassandra')

compile project(':echo-stomp')
compile project(':echo-elasticsearch')
compile spinnaker.dependency('bootActuator')
compile spinnaker.dependency('bootWeb')
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

include 'echo-core', 'echo-model', 'echo-cassandra', 'echo-web', 'echo-elasticsearch'
include 'echo-core', 'echo-model', 'echo-cassandra', 'echo-web', 'echo-elasticsearch', 'echo-stomp'

rootProject.name = 'echo'

Expand Down

0 comments on commit 72a24f5

Please sign in to comment.